How to pass variable in url to Django List View

2024/9/25 22:26:56

I have a Django generic List View that I want to filter based on the value entered into the URL. For example, when someone enters mysite.com/defaults/41 I want the view to filter all of the values matching 41. I have come accross a few ways of doing this with function based views, but not class based Django views.

I have tried:

views.py

class DefaultsListView(LoginRequiredMixin,ListView):model = models.DefaultDMLSProcessParamstemplate_name = 'defaults_list.html'login_url = 'login'def get_queryset(self):return models.DefaultDMLSProcessParams.objects.filter(device=self.kwargs[device])

urls.py

path('<int:device>', DefaultsListView.as_view(), name='Default_Listview'),
Answer

You are close, the self.kwargs is a dictionary that maps strings to the corresponding value extracted from the URL, so you need to use a string that contains 'device' here:

class DefaultsListView(LoginRequiredMixin,ListView):model = models.DefaultDMLSProcessParamstemplate_name = 'defaults_list.html'login_url = 'login'def get_queryset(self):return models.DefaultDMLSProcessParams.objects.filter(device_id=self.kwargs['device'])

It is probably better to use devide_id here, since then it is syntactically clear that we compare identifiers with identifiers.

It might also be more "idiomatic" to make a super() call, such that if you later add mixins, these can "pre-process" the get_queryset call:

class DefaultsListView(LoginRequiredMixin,ListView):model = models.DefaultDMLSProcessParamstemplate_name = 'defaults_list.html'login_url = 'login'def get_queryset(self):return super(DefaultsListView, self).get_queryset().filter(device_id=self.kwargs['device'])
https://en.xdnf.cn/q/71529.html

Related Q&A

Django Select Option selected issue

I tried to follow some examples on stackoverflow for option selected in select list but still, I could not get it work.This is my code snippet<select name="topic_id" style="width:90%&…

reading tab-delimited data without header in pandas

Im having trouble using pandas to open tab-delimited data without headers.My test data (actually contains 200 lines, of which I am showing the first 10):Tag19184 CTAAC hffef 1 a 36 - chr1…

Python Try/Except with multiple except blocks

try:raise KeyError() except KeyError:print "Caught KeyError"raise Exception() except Exception:print "Caught Exception"As expected, raising Exception() on the 5th line isnt caught i…

How to install trax, jax, jaxlib on M1 Mac on macOS 12?

trax New to trax, Im trying to run it locally (macOS 12.1, Apple Silicon ARM M1 processor, 8GB RAM, Anaconda), but Im running into some issues. In an environment with python 3.8.5, I installed trax run…

How do I match a word in a text file using python?

I want to search and match a particular word in a text file.with open(wordlist.txt, r) as searchfile:for line in searchfile:if word in line:print lineThis code returns even the words that contain subst…

Unable to Delete Videos with the Youtube Data API

Cant get deleting videos to work using the Youtube Data API. Im using the Python Client Library.All of this seems straight from the docs, so Im really confused as to why its not working. Heres my fun…

LLDB Python scripting in Xcode

Ive just discovered this handy feature of LLDB that allows me to write Python scripts that have access to variables in the frame when Im on a breakpoint in LLDB. However Im having a few issues when usi…

What technologies exist to create stand alone executables for Python 3?

Other than cx_Freeze, are there any other current maintained tool suites to generate stand alone executables for Python 3k?Are there any other techniques for minimizing preinstallation requirements un…

running multiple threads in python, simultaneously - is it possible?

Im writing a little crawler that should fetch a URL multiple times, I want all of the threads to run at the same time (simultaneously).Ive written a little piece of code that should do that.import thre…

Drawing bounding rectangles around multiple objects in binary image in python

I am trying to write some easy code in python to produce bounding rectangles around objects in a binary image, where there may be 1 or more objects. This is fairly easy to achieve with cv2.boundingRec…