how to use the href attribute in django templates
Question:
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.
First the urls.py line for the detail page
url(r'detail/(?P<jobID>d+)/$', 'appname.views.detail')
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.
Answers:
Add /
at start in href
:
<a href="/appname/detail/{{ job.id }}/">{{ job.name }}</a>
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>
From my experience, as long as you have defined the url of the page the href
tag should lead to in urls.py
, include the absolute path in the format below.
Site name: yyyy.com
Url of page to redirect to in urls.py: yyyy.com/signup/
Sample link: <a href="/signup/">Signup</a>
You will notice that what goes inside the href tag is sort of appended to the current url. For more dynamic links, you can use some python as of DTL guidelines.
Suppose u are at
myapp/detail
and u want to go at page
myapp/anything
U shoud do this
<a href="../anything">Page</a>
For django templates
Url.py File
app_name = 'app_name'
path('url/', TestView.as_view(), name='the_url'),
path('url//<int:id>', TestView.as_view(), name='the_url'),
templates(html.py)
<a href="{% url 'app_name:the_url' %}">Test</a>
<a href="{% url 'app_name:the_url' id %}">Test</a>
I got the same problem, new address is being added after the "/" URL which is not recognised.
Can be solved by adding "/" at the address you are giving at the href attribute.
ex: href:services is wrong
href:/services is to be used to go to services page.
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.
First the urls.py line for the detail page
url(r'detail/(?P<jobID>d+)/$', 'appname.views.detail')
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.
Add /
at start in href
:
<a href="/appname/detail/{{ job.id }}/">{{ job.name }}</a>
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>
From my experience, as long as you have defined the url of the page the href
tag should lead to in urls.py
, include the absolute path in the format below.
Site name: yyyy.com
Url of page to redirect to in urls.py: yyyy.com/signup/
Sample link: <a href="/signup/">Signup</a>
You will notice that what goes inside the href tag is sort of appended to the current url. For more dynamic links, you can use some python as of DTL guidelines.
Suppose u are at
myapp/detail
and u want to go at page
myapp/anything
U shoud do this
<a href="../anything">Page</a>
For django templates
Url.py File
app_name = 'app_name'
path('url/', TestView.as_view(), name='the_url'),
path('url//<int:id>', TestView.as_view(), name='the_url'),
templates(html.py)
<a href="{% url 'app_name:the_url' %}">Test</a>
<a href="{% url 'app_name:the_url' id %}">Test</a>
I got the same problem, new address is being added after the "/" URL which is not recognised.
Can be solved by adding "/" at the address you are giving at the href attribute.
ex: href:services is wrong
href:/services is to be used to go to services page.