如何对抗未找到的django错误页面(404)

sup*_*ep1 3 python django

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

该应用程序显示基本轮询,当您单击它时,它会显示一些选项和一个投票按钮.在此输入图像描述

问题是当我点击投票.它显示找不到页面.我认为问题是重定向,但我不知道在哪里确定问题.第一页是index.html,它显示问题,然后是detail.html,显示选项和问题.我知道当我点击投票时,它会回到应用程序URLconf,然后urlconf执行视图功能,视图功能执行结果.

我的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>
Run Code Online (Sandbox Code Playgroud)

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'),
)
Run Code Online (Sandbox Code Playgroud)

我的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))
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

感谢你们对我的帮助 .这将是我的第一个突破性应用程序,如果我可以让它工作.

我的主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)),
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

jpi*_*pic 6

不要硬编码网址

你应该没有硬编码URL的任何地方-只是想为文件系统路径.你不仅要杀小猫,还要让你的代码不那么稳固!

反向网址安装!

阅读有关为初学者翻转网址,使用主菜名称网址以及甜点的{%url%}模板标签的信息.

在消化时,你将成为Django url系统的主人B)

阅读教程

在您链接的教程中,它们不会对网址进行硬编码:

{% url 'polls:vote' poll.id %}
Run Code Online (Sandbox Code Playgroud)

这是要走的路!!

确保模板中的任何位置都没有硬编码的网址,您的问题就会消失.