Python 我在django中的URL有问题,我有;“未找到页面”;
我自己也开始学习Django,理解URL和路径如何工作存在问题 错误消息是: 找不到页面(404)Python 我在django中的URL有问题,我有;“未找到页面”;,python,django,django-urls,Python,Django,Django Urls,我自己也开始学习Django,理解URL和路径如何工作存在问题 错误消息是: 找不到页面(404) 请求方法:获取 请求URL:http://127.0.0.1:8000/question/1/ Django使用not_bad.URL中定义的URLconf,按以下顺序尝试了这些URL模式: [name='index'] [name='detail'] [name='results'] [name='vote'] microsanya/ 当前路径(问题/1/)与这些路径中的任何一个都不匹配。 在项
请求方法:获取
请求URL:
http://127.0.0.1:8000/question/1/
Django使用not_bad.URL中定义的URLconf,按以下顺序尝试了这些URL模式:
[name='index']
[name='detail']
[name='results']
[name='vote']
microsanya/
当前路径(问题/1/)与这些路径中的任何一个都不匹配。
在项目文件中,我有以下代码: 不坏\url.py: polls/url.py: polls/views.py
我有名为index.html和answer.html的文件,但我认为问题不在这些文件中。您需要在
urlpatterns
中提供路径
例如:
/url.py
轮询/url.py
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote' ),
]
app_name='polls'
URL模式=[
路径(“”,views.index,name='index'),
路径(“/”,views.detail,name='detail'),
路径('/results/',views.results,name='results'),
路径('/vote/',views.vote,name='vote'),
]
在本例中,应用程序的所有路径将从
/question/
开始,然后索引(精确)路径将指向索引
视图、/question/1/
到详细信息
视图等等。您需要在urlpatterns
中提供路径
例如:
/url.py
轮询/url.py
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote' ),
]
app_name='polls'
URL模式=[
路径(“”,views.index,name='index'),
路径(“/”,views.detail,name='detail'),
路径('/results/',views.results,name='results'),
路径('/vote/',views.vote,name='vote'),
]
在本例中,应用程序的所有路径将从
/question/
开始,然后索引(精确)路径将指向索引
视图、/question/1/
到详细信息
视图等等。您必须更改url模式,如:路径('question/',views.detail,name='detail'),
等。查看django文档了解更多示例:当前模式未在urlpatterns配置中定义,因此请定义它们。每个url模式都有不同的模式/模板/正则表达式question/
用于问题详细信息页面,vote/
或question/int:id/vote
用于投票等。非常感谢您的帮助,我终于了解了它的工作原理:)您必须更改url模式,如:路径('question/',views.detail,name='detail'),
等。查看django文档了解更多示例:当前模式未在urlpatterns配置中定义,因此请定义它们。每个url模式都有不同的模式/模板/正则表达式<代码>问题/对于问题详细信息页面,投票/
或问题/int:id/vote
用于投票等等。非常感谢您的帮助,我终于找到了它的工作原理:)我试过了,它成功了,非常感谢!随时乐意帮忙。你能把它标记为“接受”吗?我试过了,效果很好,非常感谢!随时乐意帮忙。你能把它标为“已接受”吗?
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.shortcuts import redirect
from django.shortcuts import render
from django.http import Http404
from . models import Question, Choice
def index(request):
question = Question.objects.all()
return render(request, "index.html", {"latest_questions": Question.objects.order_by('-pub_date')[:5]})
def detail(request, question_id):
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404
return render(request, 'polls/answer.html', {'question': question})
def answer(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
question = question.question_set.get(pk=request.POST['question'])
except (KeyError, Question.DoesNotExist):
return render(request, 'answer.html', {'question': question, 'error_message': 'Question does not exist'})
else:
if question.correct:
return render(request, "index.html", {"latest_questions": Question.objects.order_by('-pub_date')[:5], "message": "Nice! Choose another one!"})
else:
return render(request, 'answer.html', {'question': question, 'error_message': 'Wrong Answer!'})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
urlpatterns = [
path('question/', include('polls.urls')),
# ...
]
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote' ),
]