4

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.

2
  • What is in views.tags? Also [\W-] can be replaced with \W since - is already matched with \W in Python. Commented Jul 4, 2015 at 3:08
  • @Andie2302 views.tags is the view that renders the tags template. Commented Jul 4, 2015 at 3:10

1 Answer 1

7

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 %}
Sign up to request clarification or add additional context in comments.

2 Comments

Now i am getting NoReverseMatch at / Reverse for 'tags' with arguments '()' and keyword arguments '{u'slug': u'Python'}' not found. 1 pattern(s) tried: ['tags/(?P<slug>\\W+)/$']
got it in the end. The problem was with my url too. I had to remove the [\W-] and replace it with a . to make it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.