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

  • Home
  • SEARCH
  • 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 9243489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:49:18+00:00 2026-06-18T08:49:18+00:00

Here is my scenario: I have multiple select boxes on a page, where a

  • 0

Here is my scenario: I have multiple select boxes on a page, where a user selects choices from a ModelChoiceField and is re-directed to a view with the selected option(s) passed as a parameter. Something like:

class BrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

view:

def browse(request):
    thing_list = Thing.objects.all()
    if request.method == 'POST':
        form = BrowseForm(request.POST)
        if form.is_valid():
            thing = form.cleaned_data['thing']
            stuff = form.cleaned_data['stuff']
            return HttpResponseRedirect(reverse('browse_thing', kwargs={'thing':thing, 'stuff':stuff}))
    else:
        form = BrowseForm()
    return render(request, 'browse.html', {'form':form, 'thing_list':thing_list})

This requires that I submit both fields of BrowseForm, but I want so that there are two different urls for thing and stuff, where either thing or stuff is passed as the parameter, not both. Something like:

url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse', name='browse_thing'),
url(r'^browse/stuff/(?P<stuff>[\w-]+)/$', 'views.browse', name='browse_stuff'),

Is there a way to do this with one form and one view? Or do I need to write a different form for thing and stuff, then check to see which one is passed in my view and re-direct to the appropriate url accordingly? How would you do this? Thanks for your ideas!

  • 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-06-18T08:49:19+00:00Added an answer on June 18, 2026 at 8:49 am

    Although it doesn’t seem very DRY, this is how I solved the problem:

    forms.py:

    class StuffBrowseForm(forms.Form):
        stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    
    class ThingBrowseForm(forms.Form):
        thing = forms.ModelChoiceField(queryset=Thing.objects.all(), widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    

    view:

    def browse(request):
        thing_list = Thing.objects.all()
        if request.method == 'POST':
            form1 = StuffBrowseForm(request.POST)
            form2 = ThingBrowseForm(request.POST)
            if form1.is_valid():
                stuff = form1.cleaned_data['stuff']
                return HttpResponseRedirect(reverse('browse_stuffs', kwargs={'stuff':stuff}))
            elif form2.is_valid():
                thing = form2.cleaned_data['thing']
                return HttpResponseRedirect(reverse('browse_things', kwargs={'thing':thing}))
        else:
            form1 = StuffBrowseForm()
            form2 = ThingBrowseForm()
        return render(request, 'browse.html', {'form1':form1, 'form2':form2, 'thing_list':thing_list})
    

    urls:

    url(r'^browse/$', 'views.browse', name='browse'),
    url(r'^browse/stuff/(?P<stuff>[\w-]+)/$', 'views.browse_stuffs', name='browse_stuffs'),
    url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse_things', name='browse_things'),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my scenario: I have a master actor, which receives messages from multiple
Here's a scenario... I have a column. In the column i have multiple divs
Here's the scenario: I have a page that contains a video element which is
Say I have a multiple inheritance scenario: class A(object): # code for A here
Here's the scenario...A person can have a user record, with or without an associated
Here is the scenario: We have multiple clients,dealers and staff in our application. this
Here is my scenario: I have two MySQL tables: Categories (columns: id, category) Items
Here's the scenario: I have a public repo A . Bob forks A ,
Here is my scenario: I have an Eclipse project in my desktop in a
Here is the scenario: I have a visual that displays some data The data

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.