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)

Saya ingin melihat produk dengan tautan berikut:

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

Ini mengembalikan saya:

Lingkungan Hidup:

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 (panggilan terakhir terakhir): File "/home/frank/Dev/cilsa/lib/python3.8/site-packages/Django/core/handlers/exception.py", baris 47, di inner response = get_response(request ) File "/home/frank/Dev/cilsa/lib/python3.8/site-packages/Django/core/handlers/base.py", baris 181, di _get_response response = dibungkus_callback(permintaan, *callback_args, **callback_kwargs )

Jenis Pengecualian: TypeError di /products/1/

Nilai Pengecualian: dynamic_lookup_view() mendapat argumen kata kunci yang tidak terduga 'my_id'

0
Frnk 9 Mei 2021, 22:42

1 menjawab

Jawaban Terbaik

Anda telah mendefinisikan dynamic_lookup_view dua kali :) Ini mengambil definisi terakhir yang tidak memiliki "my_id" sebagai parameter. Lihat baris 26 dari products/views.py.

Saya menambahkan permintaan tarik ke repo Anda https://github.com/frnkdmn/cilsa/pull/ 1

1
Trent 13 Mei 2021, 01:44