Search code examples
pythondjangoslugify

Slugify issue in django template


I have a link in a django html template. I want to pass in a slugified string to the view for processing. I am getting an error and it is no slugifying the string. here is the code I have. Am I missing something or do i need to add something for slugify to work on the string...

<p><a href="{% url 'group_home' group.group.name|slugify %}">{{ group.group.name }}</a></p>

url:

url(r'^(?P<groupname>[\w+]+)/$', views.group_home, name='group_home'),

string example:

first group

here is the error:

NoReverseMatch at /groups/
Reverse for 'group_home' with arguments '('first-group',)' not found. 1 pattern(s) tried: ['groups/(?P<groupname>[\\w+]+)/$']

Another question I have is how do i unslugify a string once I am in the view.


Solution

  • You're missing a hyphen on your regex. It will match the hyphens of your slugified string:

    url(r'^(?P<groupname>[\w+-]+)/$', views.group_home, name='group_home'),
    

    Why would you want to "unslugify"? Maybe you're looking for something like a built-in class-based generic view. They allow you to retrieve an object based on the pk/slug specified in the url.