Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url pattern for alphanumeric(slug) string in django

I am trying to make this url patterns work, but I am not getting anywhere.I get a page not found error.The url in my url bar is http://127.0.0.1:8000/tags/?Pythonif I select python slug, but it does not use the defined url. What I want to do is, open a new template with information related to slug. This is my view:

def tags(request,slug):
    verse = get_object_or_404(Tag, slug=slug)
    return render(request, 'tags.html',{'verse':verse})

Here is my url:

 url(r'^tags/(?P<slug>[\W-]+)/$', views.tags),

This is my template:

Tagged under -  {% for i in verse.tags.all%}<a href='/tags'>{{ i }} | </a>{% endfor %}

I have tried passing a query string in template as:

/tags/q={{i}}

I have also tried to use:

{% url tags i.slug %}

(based on Slug Url Regex in Django)

But so far nothing is working. I want to open the tags template with the selected slug being displayed.

like image 936
Ekwinder Saini Avatar asked Mar 15 '23 12:03

Ekwinder Saini


1 Answers

you must set a name In url similar below:

url(r'^tags/(?P<slug>.+)/$', views.tags, name='tags'),

in template change your url tag to:

{% url "tags" slug=i.slug %}
like image 187
M.javid Avatar answered Mar 18 '23 16:03

M.javid