问题描述
当我尝试使用Django模板中的链接时,从 / appname / index /
转到 / appname / detail / ###
我改为访问 / appname / index / detail / ###
,这不是我要获取的内容,因此我的应用可以当然不能在urlconf中找到它。
When I try to use a link in my Django template from /appname/index/
to get to /appname/detail/###
I am instead getting to /appname/index/detail/###
which is not what I'm trying to get so my app can't find it in the urlconf of course.
首先是详细信息页面的urls.py行
First the urls.py line for the detail page
url(r'detail/(?P<jobID>\d+)/$', 'appname.views.detail')
另外,根urlconf
Additionally, the root urlconf
urlpatterns = patterns('',
url(r'^appname/', include('appname.urls')),
url(r'^admin/', include(admin.site.urls)),
)
接下来尝试到达的模板代码
Next the template code trying to get there
{% for job in jobList %}
<a href="detail/{{ job.id }}/">{{ job.name }}</a>
我不确定还有什么适用的信息,只问您是否想看看其他。我也尝试过:
I'm not sure what else might be applicable information, just ask if you would like to see something else. I also tried :
<a href="{% url 'appname.views.detail' %}/{{ job.id }}">{{ job.name }}</a>
但这都不起作用。预先感谢您的帮助。
But that didn't work either. Thank you in advance for any help.
推荐答案
在开始时添加 /
在 href
中:
<a href="/appname/detail/{{ job.id }}/">{{ job.name }}</a>
并使用 url
标记为您工作需要这样做:
And for the url
tag to work you need to do it like this:
<a href="{% url 'appname.views.detail' jobID=job.id %}">{{ job.name }}</a>
这篇关于如何在Django模板中使用href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!