使用定义的 URLconf,Django 尝试了这些 URL 模式
Using the URLconf defined,Django tried these URL patterns
我正在做教程 "Movie rental",但我遇到了错误。
使用 vidly.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.
我的代码是(来自主要urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
来自我的网站,即电影(urls.py)
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='movie_index'),
path('<int:movie_id', views.detail, name='movie_detail')
]
来自 views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Movie
def index(request):
movies = Movie.objects.all()
return render(request, 'movies/index.html', {'movies': movies})
def detail(request, movie_id):
return HttpResponse(movie_id)
我做错了什么?
在您的 movies.urls
中,您忘记关闭 URL 参数。
path('<int:movie_id>/', views.detail, name='movie_detail')
相反,你做到了,
path('<int:movie_id', views.detail, name='movie_detail')
您缺少结尾 >/
。
path('<int:movie_id>/', views.detail, name='movie_detail')
我正在做教程 "Movie rental",但我遇到了错误。 使用 vidly.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.
我的代码是(来自主要urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
来自我的网站,即电影(urls.py)
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='movie_index'),
path('<int:movie_id', views.detail, name='movie_detail')
]
来自 views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Movie
def index(request):
movies = Movie.objects.all()
return render(request, 'movies/index.html', {'movies': movies})
def detail(request, movie_id):
return HttpResponse(movie_id)
我做错了什么?
在您的 movies.urls
中,您忘记关闭 URL 参数。
path('<int:movie_id>/', views.detail, name='movie_detail')
相反,你做到了,
path('<int:movie_id', views.detail, name='movie_detail')
您缺少结尾 >/
。
path('<int:movie_id>/', views.detail, name='movie_detail')