2

I am setting up a simple html page, the page captures the information that the user entered and based on the information that the user entered makes a new page. The problem is that I cant get back the information entered by the user at the backed and I dont understand where I am going wrong.

My views file is setup like this:

def suggestion(request):  
    if request.method == 'POST':
        form = BusinessName(request.POST)
        if form.is_valid():
            data=form.cleaned_data  
            context = insert_function_here(data)
            return render( request,'mainpage.html', context)
    else:
        form = BusinessName()  
        context = {'form':form}
        return render( request,'mainpage.html', context) 

My forms.py is setup like this:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.HiddenInput(), required = False)

The relevant part of my html is set up like this:

<form id="user_input_form" method="post" action="http://127.0.0.1:8000/textinsighters/suggestion">
    Enter Your Business Name : <input type="text" list="browsers" name="browser" id="user_input">
    {% csrf_token %}
    {{ form }}
      <datalist id="browsers">
        <option value="Internet Explorer">
        <option value="Firefox">
        <option value="Chrome">
        <option value="Opera">
        <option value="Safari">
      </datalist>
      <button onclick="myFunction()">Submittt</button>
</form>
<script>
    function myFunction() {
document.getElementById("id_business_name").value = document.getElementById("user_input").value;                                            
document.getElementById("user_input_form").submit();
    }
</script>

I want an auto-completing list so thats why I am creating a form in html. I get the user input, set the value of the Django form field to the value that the user entered and submit it. I should get something back but the variable 'data' in views doesnt contain the user input.

Thanks

Turab Hassan
  • 73
  • 1
  • 7

1 Answers1

1

You are using a forms.HiddenInput() as the widget and then add the form field yourself. This doesn't work that way. What if you change the field class to TextInput:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.TextInput())

If you're goal is to add custom attributes to the widget, then this can be done by providing an attrs dictionary:

class BusinessName(forms.Form):
    business_name = forms.CharField(widget = forms.TextInput(attrs={
        'list': 'browser'
    }))

Or you could have a look at the django-widget-tweaks package to add attributes in the template.

Elwin Arens
  • 1,542
  • 10
  • 21
  • Thanks, works perfectly. Is it possible to define the list in the forms.py file or that needs to be defined in the html? my list is going to be big, like 100 options and thats why I want the auto complete feature. or is there a better way to implement this? – Turab Hassan Jun 03 '16 at 00:42
  • If your list can grow large one way to approach this would be to instead create a forms.CharField() and use an API source for searching that fills this field. Up to 1k options Python/Django should easily manage, but beyond that a search API would be interesting. If the answer helped you, feel free to upvote and accept it :-) – Elwin Arens Jun 03 '16 at 05:22