I'm fairly new to python and I'm trying to build a URL pattern that takes two fields as parameters. This is part of my model:
CATEGORY_CHOICES = (
('M', 'Music'),
('G', 'Games'),
('T', 'TV'),
('F', 'Film'),
('O', 'Misc'),
)
category = models.CharField(max_length = 1, choices = CATEGORY_CHOICES)
slug = models.SlugField(unique=True, max_length=255)
What I want to achieve is to be able to call a such as: thisblog.com/music/this-post where /music is the category and /this-post is the slug.
I had an attempt but I can't figure out how to do it properly:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(r'^(?P<category>[\w\-]+)/(?P<slug>[\w\-]+)/$', blog_post, name = 'blog_post'),
)
This gives me a 'NoReverseMatch at /' error.
Any help is greatly appreciated :)
UPDATE:
In my html template I have a link:
<p><a class="btn btn-default" href="{{post.get_absolute_url}}" role="button">Read more »</a></p>
get_absolute_url is defined as:
def get_absolute_url(self):
return reverse ('blog.views.post', args = [self.slug])