django已创建问题页面,当我使用动态网址时找不到

时间:2019-05-11 06:20:49

标签: django

app / urls.py

        from django.conf.urls import url
        from . import views
        urlpatterns = [
        url(r'^$', views.home, name = "home"),
        url(r'^productform/', views.product_create_view,   name="productform"),
        url(r'^products/<int:id>', views.products, name="products"),
                ]

root / urls.py

      from django.conf.urls import url, include
      from django.contrib import admin

      urlpatterns = [
      url(r'^admin/', admin.site.urls),
      url(r'', include('firstapp.urls')),
           ]

views.py --- >>

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

找不到页面错误

我的浏览器出现此错误 enter image description here

1 个答案:

答案 0 :(得分:1)

您的url(..)包含path(..) syntax [Django-doc]。您可以使用path(..)及更高版本)来解决问题;或通过将模式转换为正则表达式模式并使用url(..) [Django-doc]re_path(..) [Django-doc]及更高版本)。

因此,在及更高版本中,我们可以通过编写以下内容来解决该问题:

from django.conf.urls import path, url
from . import views

urlpatterns = [
    url(r'^$', views.home, name = "home"),
    url(r'^productform/', views.product_create_view,   name="productform"),
    path('products/<int:id>', views.products, name="products"),
]

或在中:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name = "home"),
    url(r'^productform/', views.product_create_view,   name="productform"),
    url(r'^products/(?P<id>\d+)/$', views.products, name="products"),
]