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
答案 0 :(得分:1)
您的url(..)
包含path(..)
syntax [Django-doc]。您可以使用path(..)
(django-2.0及更高版本)来解决问题;或通过将模式转换为正则表达式模式并使用url(..)
[Django-doc]或re_path(..)
[Django-doc](django-2.0及更高版本)。
因此,在django-2.0及更高版本中,我们可以通过编写以下内容来解决该问题:
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"),
]
或在django-1.x中:
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"),
]