samedi 10 février 2018

Django url variable catching

I'm having trouble at having Django urlpatterns catch a variable as an variable and instead take it as a set URL.

urlpatterns:

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^about/', views.about),
    url(r'^views/available_products/',  views.available_products),
    url(r'^views/productview/<int:product_id>/', views.productview)
]

When I host the server and go to /about/, /views/available_products/ or /admin/ they work fine, but trying to go to /views/productview/1/ gives a 404 error whereas going to /views/productview// gives a missing argument -error.

I tried to read the documentation and saw no obvious tell as to why my url variable argument wouldn't work, but clearly I'm doing something fundamentally wrong.

Here is an example error message from the debug page:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/views/productview/12/
Using the URLconf defined in firstdjango.urls, Django tried these URL patterns, in this order:

^admin/
^about/
^views/available_products/
^views/productview/<int:product_id>/
The current path, views/productview/12/, didn't match any of these.

And here is the same error message but where the URL I try with is the same as in urlpatterns:

TypeError at /views/productview/<int:product_id>/
productview() missing 1 required positional argument: 'product_id'
Request Method: GET
Request URL:    http://localhost:8000/views/productview/%3Cint:product_id%3E/
Django Version: 1.11.8
Exception Type: TypeError
Exception Value:    
productview() missing 1 required positional argument: 'product_id'
Server time:    Sat, 10 Feb 2018 12:25:21 +0000




Aucun commentaire:

Enregistrer un commentaire