【发布时间】: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)