使用定义的URLconf,Django尝试了这些URL模式

问题描述 投票:0回答:2

我正在上教程“电影租借”,但出现错误。Django使用vidly.urls中定义的URLconf,按以下顺序尝试了这些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')

]

from 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)

我做错了什么?

python django url-parameters
2个回答
1
投票

您缺少结尾>/

path('<int:movie_id>/', views.detail, name='movie_detail')

0
投票

movies.urls中,您忘记了关闭URL参数。

path('<int:movie_id>', views.detail, name='movie_detail')

代替您,

path('<int:movie_id', views.detail, name='movie_detail')
© www.soinside.com 2019 - 2024. All rights reserved.