Skip to content

Instantly share code, notes, and snippets.

@sleekslush
Created January 21, 2012 01:01
Show Gist options
  • Save sleekslush/1650547 to your computer and use it in GitHub Desktop.
Save sleekslush/1650547 to your computer and use it in GitHub Desktop.
Slug field name example with DetailView
# urls.py
urlpatterns = patterns('',
url(r'^something/(?P<slug>\d+)/$', MyView.as_view()),
)
# views.py
class MyView(DetailView):
slug_field = 'my_cool_field'
@sleekslush
Copy link
Author

If that is the sort order that you use the most, you can make it the default in the model's Meta class 😄 then you don't even need to define a special queryset

@dnoyes
Copy link

dnoyes commented Jan 21, 2012

oooOOOoo. I'm off to try that now. I can be back to needing no code in my view (for now).

@wahabAwudu
Copy link

wahabAwudu commented Jan 1, 2018

how do you invoke the get_absolute_url in the list view?
my model has this get_absolute_url:
def get_absolute_url(self):
return reverse('blog:detail', kwargs= {'slug': self.title})

my urls.py:
url(r'^(?P[-\w]+)/$', PostDetailView.as_view(), name='detail'),

and my views.py:
class PostDetailView(LoginRequiredMixin, DetailView):
model = Post
template_name = 'blog/details.html'
form_class = CommentForm
slug_field = 'title'
slug_url_kwarg = 'slug'

so in the list template, i invoke the get_absolute_url like this:
{{ articles.get_absolute_url }}
and also tried:
{% url 'blog:detail' articles.title %}

it gives the same error as:
NoReverseMatch at /blog/

Reverse for 'detail' with arguments '('Hello Third',)' not found. 1 pattern(s) tried: ['blog/(?P[-\w]+)/$']

pls help me, guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment