Django: Named URLs / same pattern, different named URLs

I have a webapp that lists all my artists, albums and songs when I click on the corresponding link. I make extensive use of generic views (object_list / detail) and named URLs, but I find annoyance. I have three templates that pretty much output the same html that looks something like this:

{% extends "base.html" %} {% block content %} <div id="content"> <ul id="starts-with"> {% for starts_with in starts_with_list %} <li><a href="{% url song_list_x starts_with %}">{{ starts_with|upper }}</a></li> {% endfor %} </ul> <ul> {% for song in songs_list %} <li>{{ song.title }}</li> {% endfor %} </ul> </div> {% endblock content %} 

My template for artists and albums looks about the same, and I would like to combine the three templates into one. The fact that my variables start with song can be easily changed to obj default value. This is my <ul id="starts-with"> named url. I do not know how to fix it. Obviously, I want it to snap to a specific album / artist / song using the named URLs in my urls.py , but I don’t know how to make this context. Any suggestions?

urls.py:

 urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views', # (r'^$', index), url(r'^artists/$', artist_list, name='artist_list'), url(r'^artists/(?P<starts_with>\w)/$', artist_list, name='artist_list_x'), url(r'^artist/(?P<artist_id>\d+)/$', artist_detail, name='artist_detail'), url(r'^albums/$', album_list, name='album_list'), url(r'^albums/(?P<starts_with>\w)/$', album_list, name='album_list_x'), url(r'^album/(?P<album_id>\w)/$', album_detail, name='album_detail'), url(r'^songs/$', song_list, name='song_list'), url(r'^songs/(?P<starts_with>\w)/$', song_list, name='song_list_x'), url(r'^song/(?P<song_id>\w)/$', song_detail, name='song_detail'), ) 
+4
source share
1 answer

You could define url patterns for the universal object_type instead of individually for artists, albums, and songs:

 urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views', # (r'^$', index), url(r'^(?P<object_type>\w+)/$', music_object_list, name='music_object_list'), url(r'^(?P<object_type>\w+)/(?P<starts_with>\w)/$', music_object_list, name='music_object_list_x'), url(r'^(?P<object_type>\w+)/(?P<object_id>\d+)/$', music_object_detail, name='music_object_detail'), ) 

Then in your template the URL tag will be

 {% url music_object_list_x object_type starts_with %} * 

You may think that you only need one view, music_object_list . If you find that you need different functions for each type of object, then call the individual functions in music_object_list .

 def music_object_list(request, object_type, starts_with=None): if object_type == 'artists': return artist_list(request, starts_with=starts_with) elif object_type == 'albums': return album_list(request, starts_with=starts_with) ... 

If you are using django.views.generic.list_detail.object_list , be sure to add object_type to the object_type dictionary. This will add object_type to the template context, allowing the url tag to work.

 extra_context = {'object_type': 'songs', ...} 

* This is the new url tag syntax for Django 1.2. For older versions, you should use a comma.

 {% url music_object_list_x object_type,starts_with %} 

See documents ( Current , 1.1 ) for more information.

+3
source

Source: https://habr.com/ru/post/1307239/


All Articles