Question

I want to pass argument (an integer) to the url in template. but I get error when I tried the following:

{% for post in posts %}
    <div id="sidebar"><a href="{% url "blogapp.views.singlepost" %}?'post_id'={{post.id}}">{{ post.title }}</a></div>

I also tried:
<div id="sidebar"><a href="{% url "blogapp.views.singlepost" %}?post_id={{post.id}}">{{ post.title }}</a></div>

I tried both. but failed. I followed these articles. but it did not help. Any suggestions. I am using django 1.6

EDIT: URLconf:

urlpatterns = patterns('blogapp.views',
    # Examples:
    # url(r'^$', 'djangoLesson.views.home', name='home'),
    url(r'^$', 'allposts',name='allposts'),
    url(r'^world/$', 'hello'),
    url(r'^all/$','allposts'),
    url(r'^all/get/(?P<post_id>\d+)/$','singlepost'),
    url(r'^language/(?P<language>[a-z\-]+)/$','language'),
    url(r'^post/(?P<slug>[-\w]+)$', 
        'view_post',
        name='blog_post_detail'),
)
Was it helpful?

Solution

Try this:

<div id="sidebar"><a href="{% url 'blogapp.views.singlepost' post_id=post.id %}">{{ post.title }}</a></div>

Since you are using template tags, you dont need {{post.id}} again inside of template tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top