Creating a custom form in Django with special options

In this post I am going to elaborate my journey of creating a html form in django and then extracting the python variables from the inserted data. After doing that we can do almost anything with this data.

We can retrieve data from the django models or we can print them out in the browser. There was a specific reason for which I wanted to do that. Actually my database was created using admin panel. There was a lot of data in the database. I just wanted to retrieve that data using the data given by the user.

The user can select the data according to the categories and I had to show that data using the given filters. So, I started the journey by using a simple form. I never knew that in Django we have to create a separate file for forms due to this, I went forward to create a simple html form in the Django templates. I spent a lot of in this and finally came to know that there existed an easier path.

Creating a custom form in Django

First of all you have to create a file named forms.py in the app on which you are working(Now many smart people suggest that we must create another app for different purposes in the particular web app but for the simpler process we will work on a single app). Now the content of the forms.py should be according to the form that we want to show as the output form. For example if we want to create a form with a name field and an age field then the content of forms.py will look like.

 

from django import forms
class nameform(forms.Form):
	name = forms.CharField()
	age = forms.IntegerField()

Now we need to need to create a view for this form. So that we can show this form.

Creating the views of the custom form

 

from .forms import nameform
def nameageform(request):
	form = nameform(request.POST or None)
	context = {
		"form" : form
	}
	return render(request, "yourapp/forms.html", context)

Now your job is to create a template named forms.html inside yourapp/templates/yourapp/

Creating template of your custom form

This is the content of the file named forms.html

{% extends 'webportal/base.html' %}
{% load staticfiles %}
{% block content %}
<h1>Form</h1>
<form method="POST" action="output/">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create Post" name="">
</form>
{% endblock %}

You can look your form working once you will set up the urls for the form in the urls.py file.

After this you have to create a function named output in the views.py that will render the views when the data is posted on the form.

 

def output(request):
	query1 = request.POST.get('name')
	return HttpResponse(query1)

Now we need to do the final step i.e. to create a url of this view. The content of the file urls.py will look like this :-

 

urlpatterns = [
    url(r'^product/output/$', views.output, name="output"),
]

Hope this post will help you to save a lot of your time. Thanks for reading and feel free to comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s