【问题标题】:django, python - master -> detaildjango,python - 主 -> 详细信息
【发布时间】:2014-04-13 19:35:45
【问题描述】:

我收到此错误:页面未找到 (404) /polls/1/ 当我按下 djangoproject 教程中主 - 详细页面的链接时(我将目录从 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 正在寻找“poll”目录? 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 和 detail.html 文件的内容是什么?
  • 好的...接下来...您的索引函数在哪里(您显示 app_index 但不显示索引),如果有不同的 html 文件,请包含它。请注意,我在这里没有看到任何“a href”链接;我原以为您错过了重命名其中一个模板。
  • 这里是views.py中的索引: def index(request): now = datetime.datetime.now() version = django.get_version() html = "ovo je index od naslovnice" 返回 HttpResponse(html)

标签: python django


【解决方案1】:

我无意中发现了问题所在。在 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>

【讨论】:

  • 如果有人可以帮助我编辑此答案以正确显示代码
  • 您可以在代码的开头和结尾使用 3 个反引号 (`) 来包装代码以对其进行格式化,或者在每行的开头使用 4 个缩进空格(在实际缩进代码之前)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多