通过民意调查教程修改它以制作博客。 (Django 1.6)
在index.html中,我可以获得一个超链接,以便在硬编码时输入正确的URL:
<h2><a href="/blog/{{ entry.id }}/">{{ entry.title }}</a></h2>
但是当我使用URL模板标签时,我得到了一个404(项目urls.py中的命名空间博客)
<h2><a href="{$ url 'blog:detail' entry.id %}/">{{ entry.title }}</a></h2>
我获得的404页面将原始Django代码传递到URL
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/%7B$%20url%20'blog:detail'%20entry.id%20%%7D/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^blog/ ^$ [name='index']
^blog/ ^(?P<entry_id>\d+)/$ [name='detail']
^blog/ ^(?P<entry_id>\d+)/comment/$ [name='comment']
^admin/
The current URL, blog/{$ url 'blog:detail' entry.id %}/, didn't match any of these.
这是博客应用中的urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<entry_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<entry_id>\d+)/comment/$', views.comment, name='comment'),
)
以下是相关观点:
def index(request):
latest_entries = Entry.objects.order_by('-pub_date')[:5]
tags = Tags.objects.filter()
context = {'latest_entries': latest_entries, 'tags': tags}
return render(request, 'entries/index.html', context)
def detail(request, entry_id):
entry = get_object_or_404(Entry, pk=entry_id)
return render(request, 'entries/detail.html', {'entry': entry})
答案 0 :(得分:4)
将{$
更改为{%
:
<h2><a href="{% url 'blog:detail' entry.id %}/">{{ entry.title }}</a></h2>
答案 1 :(得分:1)
尝试替换模板标记,而不是
{$ url 'blog:detail' entry.id %}
写
{% url 'blog:detail' entry.id %}
注意美元符号并使用百分号