python - Django: URL pattern to include values from multiple fields -
I'm quite new to the python and I'm trying to create a URL pattern which takes two fields as a parameter is. This is part of my model:
CATEGORY_CHOICES = (('M', 'Music'), ('G', 'Games'), ('T', 'TV') Slug = models.SlugField (unique = true, max_length = 255), ('F', 'movie'), ('o', 'mixed')) category = = model.carfield (max_long = 1, option = category_CHOICES) / Code> What I want to achieve, as it is to be able to call: thisblog.com/music/this-post where / music category and / this post is slug. / P>
I had an attempt but I do not understand how to do it properly:
urlpatterns = patterns ('', url (r '^ admin / ', (Admin.site.urls), url (r' ^ $ ',' blog.views.index '), url (r' ^ (? P & lt; class & gt; [\ w \ -] + )
P> This one gives me a link on 'NoReverseMatch'
update:
I have a link in my html template:
get_absolute_url is defined:
def get_absolute_url (self): reverse reverse ('blog.views.post', args = [self. Slug]} << Code>
There are two errors in your get_absolute_url method First of all, you have to use the name attribute of the URL because you have defined it. And secondly, you have to provide both the criteria: class as well as the slug. Then it should be:
reverse reverse ('blog_post', args = [self category, self.slug]) < / Div>
Comments
Post a Comment