0

django 3.2.2

urls.py

from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, render_initial_data, dynamic_lookup_view

urlpatterns = [
    path('products/<int:my_id>/', dynamic_lookup_view, name='product'),
    path('', home_view, name='home'),
    path('about/', about_view),
    path('contact/', contact_view),
    path('create/', product_create_view),
    path('initial/', render_initial_data),
    path('product/', product_detail_view),
    path('admin/', admin.site.urls),
]

views.py

from django.shortcuts import render, get_object_or_404
from .forms import ProductForm, RawProducForm
from .models import Product
# Create your views here.

def dynamic_lookup_view(request, my_id):
    obj = Product.objects.get(id=my_id)
    context = {
        "object": obj
    }
    return render(request, "products/product_detail.html", context)

Yo quiero ver el producto con el siguiente link

http://127.0.0.1:8000/products/2/

me retorna:

Environment: Request Method: GET Request URL: http://127.0.0.1:8000/products/1/ Django Version: 3.2.2 Python Version: 3.8.5 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'pages', 'products'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Traceback (most recent call last):

File "/home/frank/Dev/cilsa/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request)

File "/home/frank/Dev/cilsa/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

Exception Type: TypeError at /products/1/

Exception Value: dynamic_lookup_view() got an unexpected keyword argument 'my_id'

3
  • En ese caso en Django suelo utilizar DetailView.
    – ret
    Commented el 9 may. 2021 a las 20:04
  • Si pudieras compartir el stacktrace completo seria de ayuda. Commented el 10 may. 2021 a las 13:28
  • @KevinRamirezZavalza actualizado
    – Frnk
    Commented el 10 may. 2021 a las 14:48

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.