1

I have a database 'artist' with one table : 'name', 'style'. Users can add any names for an artist.

I would like to pass an argument to url in my template, like this :

<a href="{% url 'webgui.views.music' artist.name %}" style="margin-bottom: 3px" type="button"

But is it possible to set dynamically the URL with the argument 'artist.name' (in urls.py) ?

My urls.py actually :

url(r'^music/(\d+)/$', 'webgui.views.music'),

Maybe I need to change '\d+' by another regexp ??

5
  • you want to use a variable to select the named url? eg a variable containing 'webgui.views.music' ? Commented Oct 13, 2014 at 16:29
  • according to docs that is possible docs.djangoproject.com/en/dev/ref/templates/builtins/#url Commented Oct 13, 2014 at 16:30
  • No. Like this in Django URLs doc : url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'), , except I don't know the length variable.. Thanks for the link. Commented Oct 13, 2014 at 16:35
  • I don't understand what is your question... are you just trying to work out the correct regex in the url for accepting an artist name? Commented Oct 13, 2014 at 16:37
  • This is very unclear. artist.name is already a variable. What exactly are you trying to do? Commented Oct 13, 2014 at 16:37

1 Answer 1

3

Instead of using

url(r'^music/(\d+)/$', 'webgui.views.music'),

because d+ stands for digits, you should use

url(r'^music/?P<artist_name>[a-zA-Z0-9 \'&-]/$', 'webgui.views.music')

Don't forget to modify function

def whatever_named_it(request, artist_name):
    ...

Update: In template you should use

{% url 'webgui.views.music' artist_name=artist.name %}

because we are using named arguments.

Extra: If you are going to allow any text for artist's name, I would recommend you using slug to avoid spaces in URL. That would make it better to read, search engine friendly and avoid of insane user input. When programming web-app never trust user input ... ever.

For "slugified" name urls would be:

url(r'^music/(?P<slug>[\w-]+)/$', 'webgui.views.music')
Sign up to request clarification or add additional context in comments.

5 Comments

[a-zA-Z0-9 \'&-] is extremely ethnocentric as a regular expression to capture names.
Thank you, gcerar, it was about I've asked. But it doesn't works (NoReverseMatch), and I'm not sure it will be my final solution.
Isador, can you try url(r'^music/(?P<artist_name>[a-zA-Z0-9 \'&-])/$', 'webgui.views.music') instead and report if it's working?
The same : Exception Value: Reverse for 'webgui.views.music' with arguments '(u'Anathema',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ["music/(?P<artist_name>[a-zA-Z0-9 \\'&-])/$"]
Ooooh right. Sorry I forgot we used named argument. In template replace {% url 'webgui.views.music' artist.name %} with {% url 'webgui.views.music' artist_name=artist.name %}. I think that should do it.

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.