Home > Back-end >  Fetching html form data in django
Fetching html form data in django

Time:06-11

I have created one html form for my web application. Now I want to fetch data for further validation on submit the form in DJANGO. So how can I do this. I have attached my html form code below. Actually, I can get the data by the request.POST.get('field_name') method but I want to get data in the single object. Also I want to create seprate python file for validation. So how can I redirect on that file.

<form action="/contact_form_data_insert" name = "contact_us_form" id = "contact_us_form" method="POST">
    <div >
        <div >
            <div >

                <div >
                    <div  >
                      <input type="text" id="contact_name" name="contact_name"  placeholder = "Your Name"/>
                    </div>
                </div>


                <div >
                    <div  >
                      <input type="text" id="contact_company" name="contact_company"  placeholder = "Your Company" />
                    </div>
                </div>
            </div>


            <div >

                <div >
                    <div  >
                      <input type="tel"  id="contact_phone_number" name="contact_phone_number" class = "form-control form-control-lg-border-0" placeholder = "Phone Number">
                    </div>
                </div>


                <div >
                    <div  >
                      <input type="email" id="contact_email" name="contact_email"  placeholder = "Email"/>
                    </div>
                </div>
            </div>
        </div>




        <div >
            <div >
                    <div >
                        <div  >
                          <input type="text" id="contact_subject" name = "contact_subject"  placeholder = "Subject" />
                        </div>
                    </div>
            </div>
           
        </div>


        <div >
            <div >
                    <div >
                        <div  >
                            <textarea  id="contact_question" name="contact_question" placeholder = "Your Question" rows="5"></textarea>
                        </div>
                    </div>
            </div>
        </div>

        <div >
            <div >
                <div >
                    <input type="submit" class = "btn btn-info btn-lg btn-block"style="color: white;font-family: serif; font-weight: bolder; "  value="SEND">
                </div>
            </div>
        </div>

        
    </div>
</form>

CodePudding user response:

Make Django form in the backend for example login form has username and password fields Don't use it in the front Just make your own form in the front end with fields named username and password Django of course can't tell if the data sent from Django form or custom form So it'll treat it normally

For example

class LoginForm(Form):
 username = ...
 password = ...
 class Meta:
   fields = ('username', 'password')

Now in the backend

form = LoginForm (request.POST)
if form.is_valid():

The form only needs data named username and password from the front side

CodePudding user response:

About using Django forms, here's a quick demo... You can create a file within your app folder called forms.py and place the following inside of it.

from django import forms

class ContactForm(forms.Form):  
     # You can also modify the field attributes from here if you wish to
     contact_name = forms.CharField(widget=forms.TextInput(attrs={'id':'contact_name', 'name': 'contact_name', 'class': 'form-control form-control-lg-border-0', 'placeholder': 'Your Name'}))

     # You can take the same approach for the other text & email input fields below
     contact_company = forms.CharField(widget=forms.TextInput(attrs={...}))
     contact_phone_number = forms.CharField(widget=forms.TextInput(attrs={...}))
     contact_subject = forms.CharField(widget=forms.TextInput(attrs={...}))
     contact_email = forms.EmailField(widget=forms.EmailInput(attrs={...}))

     # Textarea field
     contact_question = forms.CharField(widget=forms.Textarea(attrs={'id':'contact_question', 'name': 'contact_question', 'class': 'form-control form-control-lg-border-0','placeholder': 'Your Question', 'rows': '5'}))

     # Can create any method in here to handle a specific process if the form is valid
     def save(self):
         pass

From your views.py file within the same app folder:

from .forms import ContactForm  # Importing the custom form you created

# Now let's say I want to pass it to a specific view to allow a user to submit his/her information...
def contact_form_view(request):
     if request.method == 'POST':
          form = ContactForm(data=request.POST)

          if form.is_valid():
               # Do something here to process and save the form data
               form.save()
          else:
               print(form.errors)
     else:
          form = ContactForm()
     
     return render(request, 'contact_form.html', {'form': form})

Now, within your html template, you could just replace the respective input fields with the form object fields. For example:

<form method="POST" ...>
     # Where you have these fields
     <input type="text" id="contact_name" name="contact_name"  placeholder = "Your Name"/>

     # Just replace with... (i.e.: contact_name as above)
     {{ form.contact_name }}

     # Apply the above with the other respective fields.
</form>

I trust you already know how to set up your url for the view. In a nutshell, that's an approach you could take.

I do recommend that you read the doc for more information on the Django forms though.

  • Related