gpt4 book ai didi

python - 我如何解决此 Django 错误页面未找到 (404)

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:36 28 4
gpt4 key购买 nike

我正在使用教程创建此 Django 应用程序,我已完成第 4 部分 https://docs.djangoproject.com/en/dev/intro/tutorial04/

该应用程序显示一个基本投票,当您单击它时,它会显示一些选项和一个投票按钮。 enter image description here

问题出在我点击投票时。它显示找不到页面。我认为问题是重定向,但我不知道在哪里查明问题。第一页是显示问题的 index.html,然后是显示选项和问题的 detail.html。我知道当我点击投票时,它会返回到应用程序 URLconf,然后 urlconf 执行 View 函数, View 函数执行结果。

我的 detail.html 是

 <h1>{{ poll.question }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="/myapp/{{ poll.id }}/vote/" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

myapp 中的 urls.py 是:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings

from django.conf.urls import patterns, include, url

urlpatterns = patterns('myapp.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

我的 views.py 是:

from django.http import HttpResponse
from myapp.models import Poll ,choice
from django.template import Context, loader
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext

def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('myapp/index.html', {'latest_poll_list': latest_poll_list})

def results(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('myapp/results.html', {'poll': p})

def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('myapp/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance=RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('myapp.views.results', args=(p.id,)))

def detail(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
return render_to_response('myapp/detail.html', {'poll': p},
context_instance=RequestContext(request))

我的 results.html 是:

<h1>{{ poll.question }}</h1>

<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="/myapp/{{ poll.id }}/">Vote again?</a>

谢谢你帮助我。如果我能让它发挥作用,这将是我的第一个突破性应用。

我的主要 URLconf 是:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^polls/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)

enter image description here

最佳答案

不要硬编码 url

您应该在任何地方对 URL 进行硬编码 - 就像文件系统路径一样。您不仅在杀死小猫,而且还降低了代码的可靠性!

已安装反向网址!

了解 reversing urls对于初学者,using named urls主菜,关于{% url %} templatetag甜点。

消化的时候,你就是Django url系统的高手B)

阅读教程

在您链接的教程中,他们不会对 url 进行硬编码:

{% url 'polls:vote' poll.id %}

这就是要走的路!!

确保您的模板中的任何地方都没有硬编码 url,您的问题就会消失。

关于python - 我如何解决此 Django 错误页面未找到 (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com