Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4103712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:59:37+00:00 2026-05-20T20:59:37+00:00

I have a view in django that can accept a number of different filter

  • 0

I have a view in django that can accept a number of different filter parameters, but they are all optional. If I have 6 optional filters, do I really have to write urls for every combination of the 6 or is there a way to define what parts of the url are optional?

To give you an example with just 2 filters, I could have all of these url possibilities:

/<city>/<state>/
/<city>/<state>/radius/<miles>/
/<city>/<state>/company/<company-name>/
/<city>/<state>/radius/<miles>/company/<company-name>/
/<city>/<state>/company/<company-name>/radius/<miles>/

All of these url’s are pointing to the same view and the only required params are city and state. With 6 filters, this becomes unmanageable.

What’s the best way to go about doing what I want to achieve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:59:38+00:00Added an answer on May 20, 2026 at 8:59 pm

    One method would be to make the regular expression read all the given filters as a single string, and then split them up into individual values in the view.

    I came up with the following URL:

    (r'^(?P<city>[^/]+)/(?P<state>[^/]+)(?P<filters>(?:/[^/]+/[^/]+)*)/?$',
     'views.my_view'),
    

    Matching the required city and state is easy. The filters part is a bit more complicated. The inner part – (?:/[^/]+/[^/]+)* – matches filters given in the form /name/value. However, the * quantifier (like all Python regular expression quantifiers) only returns the last match found – so if the url was /radius/80/company/mycompany/ only company/mycompany would be stored. Instead, we tell it not to capture the individual values (the ?: at the start), and put it inside a capturing block which will store all filter values as a single string.

    The view logic is fairly straightforward. Note that the regular expression will only match pairs of filters – so /company/mycompany/radius/ will not be matched. This means we can safely assume we have pairs of values. The view I tested this with is as follows:

    def my_view(request, city, state, filters):
        # Split into a list ['name', 'value', 'name', 'value']. Note we remove the
        # first character of the string as it will be a slash.
        split = filters[1:].split('/')
    
        # Map into a dictionary {'name': 'value', 'name': 'value'}.
        filters = dict(zip(split[::2], split[1::2]))
    
        # Get the values you want - the second parameter is the default if none was
        # given in the URL. Note all entries in the dictionary are strings at this
        # point, so you will have to convert to the appropriate types if desired.
        radius = filters.get('radius', None)
        company = filters.get('company', None)
    
        # Then use the values as desired in your view.
        context = {
            'city': city,
            'state': state,
            'radius': radius,
            'company': company,
        }
        return render_to_response('my_view.html', context)
    

    Two things to note about this. First, it allows unknown filter entries into your view. For example, /fakefilter/somevalue is valid. The view code above ignores these, but you probably want to report an error to the user. If so, alter the code getting the values to

    radius = filters.pop('radius', None)
    company = filters.pop('company', None)
    

    Any entries remaining in the filters dictionary are unknown values about which you can complain.

    Second, if the user repeats a filter, the last value will be used. For example, /radius/80/radius/50 will set the radius to 50. If you want to detect this, you will need to scan the list of values before it is converted to a dictionary:

    given = set()
    for name in split[::2]:
        if name in given:
            # Repeated entry, complain to user or something.
        else:
            given.add(name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Django view that just returns URL parameters, but if I
I have the view function in django that written like a dispatcher calling other
I have a django view that searches my database for a name that includes
I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have 2 forms in my Django view. How can I do a check
I have a Django view that receives POSTs which do not need to have
Suppose you have a Django view that has two functions: The first function renders
I have a django view that I want to return an Excel file. The
I have the downlaod link in my Django view , where the user can
I have a Django project that, on one page, has multiple forms (in different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.