我正在这样做tutorial。不幸的是,它适用于其他版本的Django(我使用的是最新版本)。我发现了一个错误404的小问题,但我不知道如何解决。我正在寻求帮助来解决它。
启动页面“ http://127.0.0.1:8000/reviews/”时出错:
Using the URLconf defined in winerama.urls, Django tried these URL patterns, in this order:
^$ [name='review_list']
^review/(?P<review_id>[0-9]+)/$ [name='review_detail']
^wine$ [name='wine_list']
^wine/(?P<wine_id>[0-9]+)/$ [name='wine_detail']
admin/
The current path, reviews/, didn't match any of these.
我的views.py(应用程序)
from django.shortcuts import get_object_or_404, render
from .models import Review, Wine
def review_list(request):
latest_review_list = Review.objects.order_by('-pub_date')[:9]
context = {'latest_review_list':latest_review_list}
return render(request, 'reviews/review_list.html', context)
def review_detail(request, review_id):
review = get_object_or_404(Review, pk=review_id)
return render(request, 'reviews/review_detail.html', {'review': review})
def wine_list(request):
wine_list = Wine.objects.order_by('-name')
context = {'wine_list':wine_list}
return render(request, 'reviews/wine_list.html', context)
def wine_detail(request, wine_id):
wine = get_object_or_404(Wine, pk=wine_id)
return render(request, 'reviews/wine_detail.html', {'wine': wine})
我的urls.py(应用程序)
from django.conf.urls import url
from . import views
app_name = 'reviews'
urlpatterns = [
# ex: /
url(r'^$', views.review_list, name='review_list'),
# ex: /review/5/
url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
# ex: /wine/
url(r'^wine$', views.wine_list, name='wine_list'),
# ex: /wine/5/
url(r'^wine/(?P<wine_id>[0-9]+)/$', views.wine_detail, name='wine_detail'),
]
我的urls.py
from django.contrib import admin
from django.urls import path, include
app_name = 'reviews'
urlpatterns = [
path('', include('reviews.urls', namespace="reviews")),
path('admin/', admin.site.urls),
]
如何正确更正代码。我了解该错误位于urls.py(应用程序)中。任何帮助将不胜感激。
答案 0 :(得分:3)
我认为您可以在“ http://127.0.0.1:8000/”上看到评论。
如果您想在'http://127.0.0.1:8000/reviews'看到review_list,则可以尝试一下。
from django.contrib import admin
from django.urls import path, include, url
app_name = 'reviews'
urlpatterns = [
# path('', include('reviews.urls', namespace="reviews")),
# path('admin/', admin.site.urls),
url(r'^reviews/', include('reviews.urls', namespace="reviews")),
url(r'^admin/', include(admin.site.urls)),
]