-
-
Save sleekslush/1650547 to your computer and use it in GitHub Desktop.
# urls.py | |
urlpatterns = patterns('', | |
url(r'^something/(?P<slug>\d+)/$', MyView.as_view()), | |
) | |
# views.py | |
class MyView(DetailView): | |
slug_field = 'my_cool_field' |
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
oooOOOoo. I'm off to try that now. I can be back to needing no code in my view (for now).
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
This is the final version of what I ended up with: