I'm trying to use one view to resolve two url patterns, by using optional parameters in the view definition. I'm following recomendations from this post and this other question.
Here's my code for the url patterns urls.py:
urlpatterns = patterns('myapp.views',
url(r'^(?P<slug>[\w-]+)/$', 'my_view', name='main_url'),
url(r'^(?P<slug>[\w-]+)/(?P<optional>[\w-]*)/$', 'my_view', name='optional_url'),
)
And the definition of the view:
def my_view(request, slug, optional=None):
Everything works fine, as far as displaying the templates according to the url patterns. However, when I try to display links using model permalinks, it breaks. I'm following the way of getting a model absolute url as explained in django docs.
Here's the code of my model:
class MyModel(models.Model):
name = models.CharField(max_length=128)
slug = models.CharField(max_length=32)
@models.permalink
def get_absolute_url(self):
return ('main_url', [self.slug])
The problem is that get_absolute_url returns an url with two arguments. So I'm getting something like this domain.com/slug// instead of this domain.com/slug/
How can I get the absolute url without the second argument? Is there something I'm doing wrong?
Thanks