I want to make a variable in my views.py which changes depending the name of the urlpattern used

Zorgan

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?

ger.s.brett

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.

edited at
0

Comments

0 comments
Login to comment

Related

My variable changes (though I don't want to) once I change the other variable I used to set it up

Variable that changes depending on the field being used

I want to make a record of all the changes of my array - Java

I want to make a responsive banner, that changes depending of the resolution screen with php, js and html

I want to run my else statement in (views.py) but when server is run it automatically go to the if statement?

I have 2 radio buttons and i want to make a different ajax page load into a div element depending on which one is selected

i want to execute an sql query in ruby to use the result to make some tests in my views

I want to display images in my views in laravel

ComponentDidMount always changes my component state to loading which i don't want in enzyme

In my Angular JS application, I want to get know which services, modules are used in controllers?

i want to rerender my component which used cache data by reactquery in react

I want to get the root folder in a Path which is stored in a variable using groovy and used this root value in if statement

I want to make a type bot which will join my classes automatically i have tried it but got the error

Use different variable depending on which view was used in Rails 4

How do I take a variable from views.py in Django and display it on my HTML page

How can I create a function in which a value of variable changes after used once?

How do I combine one static string and a variable name to make one variable name which is defined?

I want to display my variable which i passed form php to script tag in the laravel blade page

How to make a variable that changes depending on user input in batch

I want only all cities of "INDIA" to show through ngAutocomplete..??? which changes I have to make in ngAutocomplete..?

I want the column which has NULL in my database table to make it blank

Python: i want to make a variable that stores decrementing my 'limit' variable but it's not decrementing

I want to use the variable declared inside an SSH session to be used locally in my shell script

I need to make a method which changes the status from false to true in my MongoDB C#

How do I make two unordered list fade in and fade out when my menu is clicked depending on which is showing?

I want to store my url in variable name "url" to saved the url in excel sheet csv

I want to make a my own button which has 2 other button inside and I want to make them transparent but transparency doesn't work

Can I change the name of a variable depending on another variable?

I want to add a server to my domain name