ここに私のコードです。どのurlpatternが選択されても、それの名前はurl
としてviews.pyに保存されます。クエリセットフィルタ()で使用されます。使用しているurlパターンの名前に応じて、私のviews.pyの変数を変更したい
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'),
)