Here's my code. whatever urlpattern is chosen: I want the name of it to be stored as url
in views.py. Which is then used in queryset filter().
urls.py
url(r'^news/', BoxesView.as_view(), name='news'),
url(r'^sport/', BoxesView.as_view(), name='sport'),
url(r'^cars/', BoxesView.as_view(), name='cars'),
views.py
class BoxesView(ListView):
url = #urlname to go here
def get_queryset(self):
queryset_list = Post.objects.all().filter(category=url)
models.py
category = models.CharField(choices=CATEGORY_CHOICES)
choices.py
CATEGORY_CHOICES = (
('1', 'news'),
('2', 'sport'),
('3', 'cars'),
)
Any idea?
I would replace your url.py by something like this:
url(r'(?P<keyword>\w+)/$', BoxesView.as_view())
This changes your address into an url parameter which you can then access the in your methods like this:
def get_queryset(self):
url = self.kwargs['keyword']
queryset_list = Post.objects.all().filter(category=url)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments