-
-
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' |
Aha! Okay. That makes perfect sense. Django docs are pretty great, but I was having trouble finding documentation on DetailView
.
I didn't realize at first that slug_field
was a keyword param for DetailView
, and slug
or pk
needs to be present in the regex.
Since my model has a field literally named slug
(as in, "the url-slug-ified version of the team name"), your followup comment is exactly what I needed.
So the URL will look like:
/teams/team-awesome-pants
And I needed to take team-awesome-pants
and query against the slug
field of my Team
model (which is NOT the primary key).
It's all comin' together now and starting to make sense. ;)
Yep, you got it 🤘 Yeah, with the new class-based views, I find myself looking at the source code a lot. It's pretty simple to follow but let me know if you run into more issues, I've been doing these for a while now
Your help is much appreciated. I had a simple view method defined with 2 lines, but I like learning the shortcuts too (even though it's a bit of brain melting upfront).
Yeah, the shortcuts are really just a way to set the class-level variables versus on a per-instance basis. So if you can do it at class-level, it's better. Use instance-level if you have a special case.
This is the final version of what I ended up with:
# teams/url.py
from django.conf.urls.defaults import patterns
from teams.views import TeamList, TeamDetail
urlpatterns = patterns('',
(r'^/?$', TeamList.as_view()),
(r'^(?P<slug>[\w-]+)/*$', TeamDetail.as_view()),
# views.py
from django.views.generic import DetailView, ListView
from teams.models import Team
class TeamList(ListView):
queryset = Team.objects.order_by('-name')
class TeamDetail(DetailView):
model = Team
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
All you should need is this in your example, as long as your
Team
model has a column namedslug
.(r'^(?P<slug>[\w-]+)/*$', DetailView.as_view(model=Team))