Django: Find amount of parameters starting with the same word

django url pass parameter to view
django url multiple parameters
django url parameters in template
django optional url parameter
django get url parameters
django-filter
pass multiple parameters in url django
django queryset get field value

In my Django app's views.py I want to write a function to find the amount of optional parameters that begin with 'item' that are in the request

def get_items(request):

Note: The request passed into this function could have various amounts of optional parameters starting with 'item' with a number like so: &item1=, &item2=, &item3=..etc along with other optional parameters that don't start with 'item'

So a request could be similar to any of these:

/myapp/data/&item1=foo&item2=bar&item3=baz&otherParam=other
/myapp/data/&item1=baa&otherParam=other
/myapp/data/&item1=bla&item2=baa&otherParam=other

I know I could do the following (but this would require me to know the number of 'items' ahead of time):

request.GET.get('item1')
request.GET.get('item2')
request.GET.get('item3')

Is there a way to find out the number of 'item' optional parameters? (So 3 in the first request above, 1 in the second and 2 in the third)

You can just use len(request.GET) to find count of key-value pairs in request.GET object.

URL dispatcher | Django documentation, Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming To prevent multiple URLs from mapping to the same page, dashes must be included and letters Consider the following URL patterns which optionally take a page argument: In other words, all request methods – POST , GET , HEAD , etc. A number (or variable) containing the number of paragraphs or words to generate (default is 1). method: Either w for words, p for HTML paragraphs or b for plain-text paragraph blocks (default is b). random: The word random, which if given, does not use the common paragraph (“Lorem ipsum dolor sit amet…”) when generating text.

len([value for key, value in request.GET.items() if 'item' in key.lower()])

Making queries | Django documentation, Updating a ForeignKey field works exactly the same way as saving a normal The first is a base QuerySet containing all entries that contain a headline starting with “What”. In this case, the value parameter is expected to contain the raw value of the The use of pk isn't limited to __exact queries – any query term can be  For Django class-based views we access an appropriate view function by calling the class method as_view (). This does all the work of creating an instance of the class, and making sure that the right handler methods are called for incoming HTTP requests. View (class-based) We could quite easily write the book list view as a regular function

A QueryDict responds to all the usual dict methods, so you can iterate over the keys directly - combine that with a list comprehension and call len on it:

len(k for k in request.GET if k.startswith('item'))

Or, if you actually need the matching values, use items with a dict comprehension:

items = {k: v for k, v in request.GET.items() if k.startswith('item')}

Note, you haven't shown how your frontend is generating these values but you probably don't want to do this in the first place. A QueryDict can have multiple values for each key, so you could send all the data in the same item parameter:

/myapp/data/?item=foo&item=bar&item=baz&otherParam=other

Now you can just do request.GET.getlist('item') to get ['foo', 'bar', 'baz'].

django check within template if the current url has a particular word , Django template, if tag based on current URL value. only I want to check if the url has a word in it - you know, like if a.find(b) == -1. how can i do  Working with Filter. Easily the most important method when working with Django models and the underlying QuerySets is the filter() method, which allows you to generate a QuerySet of objects that match a particular set of filtered parameters. For example, our application has a Book model with a few basic fields: title, author, and date_published.

Django, how to use filter to check if string field is contained in , You'll have to split your input into words (depending on your definition of "word"), and then iterate over them, concatenating the various  Starting with a URL requested by the user/browser, it calls the right Django view providing any arguments it might need with their values as extracted from the URL. Starting with the identification of the corresponding Django view plus the values of arguments that would be passed to it, obtain the associated URL.

Beginning Django E-Commerce, This value is used by your Django project for secret key hashing algorithms, for security Of course, if you find that the value is an empty string, you should certainly put If user-supplied parameters that are passed to the database in WHERE the user entered the word “guitar” and this was the SQL that was constructed  For each model field that has choices set, Django will add a method to retrieve the human-readable name for the field’s current value. See get_FOO_display () in the database API documentation. Note that choices can be any sequence object – not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself

Chapter 8: Advanced Views and URLconfs, Django offers another way of specifying the view function for a particular We can factor out that common prefix and pass it as the first argument to patterns() , like this: Sometimes you'll find yourself writing view functions that are quite similar, With this in mind, you can start making higher-level abstractions of your views. The find() method takes two optional, additional parameters: a start index and a stop index, indicating where in the string the search should begin and end. For instance, string .find("abc", 10, 20) will search for the substring "abc", but only from the 11th to the 21st character.

Comments
  • I just updated my question. I forgot to mention there are other optional parameters that don't start with 'item'
  • To help readers understand your answer, consider including a short description of your code and how it solves the problem