当前位置: 代码迷 >> python >> Django,Python-主->详细信息
  详细解决方案

Django,Python-主->详细信息

热度:132   发布时间:2023-07-14 08:59:20.0

我收到此错误:当我按djangoproject教程中的master-详细信息页面上的链接时,找不到页面(404)/ polls / 1 /(我将目录从poll更改为naslovnica):

views.py:

def app_index(request):
    latest_poll_list = Poll.objects.all()[:5]
    context = {'latest_poll_list': latest_poll_list}
    return render(request, 'naslovnica/index.html', context)

def detail(request, poll_id):
    try:
        poll = Poll.objects.get(pk=poll_id)
    except naslovnica.DoesNotExist:
        raise Http404
    return render(request, 'naslovnica/detail.html', {'poll': poll})

这是urls.py:

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


    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),
)

有人可以用简单的语言向我解释django为什么要查找“民意调查”目录吗? Django(1.6)中的主页面/详细页面的机制是什么?

编辑:index.html竞赛(此页面完全显示)

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

detail.html:

{{poll}}

我不小心发现了问题所在。 在index.html代码中:

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/poll/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

线

<li><a href="/poll/{{ poll.id }}/">{{ poll.question }}</a></li>

应该:

<li><a href="/naslovnica/{{ poll.id }}/">{{ poll.question }}</a></li>
  相关解决方案