Wednesday, October 25, 2017

Passing 3 user inputs as argument in url, for a search form

I am building a search form where the user inputs 3 values on a form, and I search the database for all records that match the 3 inputs.  Right now I am stuck on the user input part.

If you look at my urls.py, you will see that I tried to put 3 keywords arguments in there.  Then I tried to pass the 3 keyword arguments in the url tag with variables from my forms.py.  I think this is where things went wrong but I'm not sure.  I don't know if the .html file can use variables from forms.py

Basically what I'm trying to do is to take the 3 user inputs, and pass them as arguments in the URL.  Right now I'm getting:

NoReverseMatch at /

Reverse for 'search-results' with keyword arguments '{'pt': '', 'nb': '', 'nw': ''}' not found. 1 pattern(s) tried: ['search?(?P<pt>\\w+)&(?P<nb>\\w+)&(?P<nw>\\w+)']


urls.py
url(r'^search?(?P<pt>\w+)&(?P<nb>\w+)&(?P<nw>\w+)', views.IndexSearchResults.as_view(), name='search-results')


index.html 
<form action="{% url 'search-results' pt=property_type nb=number_of_bedrooms nw=number_of_washrooms %}" method="get">
    {{ form.as_p }}
</form>


views.py
class IndexSearchResults(generic.ListView):
    template_name = 'search-results.html'


models.py
Class BuyerListing(models.Model):
     BEDS_OPTION = (
        (BEDS_0, '0 [Studio/Bachelor]'),
        (BEDS_1, '1'),
        (BEDS_1_1, '1+1'),
        (BEDS_2, '2'),
        (BEDS_2_1, '2+1'),
        (BEDS_3, '3'),
        (BEDS_3_1, '3+1'),
        (BEDS_4, '4'),
        (BEDS_5, '5'),
        (BEDS_5_1, '5+'),    
    )

     WASH_OPTION = (
        (WASH_1, '1'),  
        (WASH_2, '2'),  
        (WASH_3, '3'),  
        (WASH_4, '4'),  
        (WASH_5, '5'),  
        (WASH_5_1, '5+'),  
    )

    PROPERTY_TYPE = (
        (CONDO_APARTMENT, 'Condo Apartment'),
        (DETACHED_HOUSE, 'Detached House'),
        (SEMI_DETACHED, 'Semi-detached'),
        (TOWNHOUSE, 'Townhouse'),
    )

    property_type = models.CharField(max_length=50)
    number_of_bedrooms = models.CharField(max_length=50, default=BEDS_0)
    number_of_washrooms = models.CharField(max_length=50, default=WASH_1)


forms.py
class MainSearch(forms.Form):

    property_type = forms.ChoiceField(choices=BuyerListing.PROPERTY_TYPE)
    
    number_of_bedrooms = forms.ChoiceField(choices=BuyerListing.BEDS_OPTION)
    
    number_of_washrooms = forms.ChoiceField(choices=BuyerListing.WASH_OPTION)
  

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/deaaeafb-c8b9-45fc-9553-1c495c65dc28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment