Getting user input from an HTML form in Django
Django is a high-level Python web framework that enables the rapid development of secure and scalable web applications. One of the most common tasks in web development is collecting user input through forms. In this blog, we will go through the process of getting user input from an HTML form in Django.
Step 1: Create a Form in HTML
The first step in getting user input is to create a form. In Django, we can use the built-in form module to generate HTML forms. However, in this tutorial, we will create a simple HTML form and later connect it to Django.
The following code creates a simple HTML form with two text fields, "Name" and "Email".
<form action="" method="post">
{% csrf_token %}
<label for="name">Name:</label>
<input type="text" id="name" name="uname">
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="uemail">
<br><br>
<input type="submit" value="Submit">
</form>
We use the name
attribute to get details in the backend
Step 2: Create a View
The next step is to create a view in Django that will handle the form submission. A view is a Python function that takes a web request and returns a web response.
To create a view in Django, we need to import the render
method from the django.shortcuts
module and define a function that takes a request as a parameter.
from django.shortcuts import render
def form_view(request):
return render(request, 'form.html')
Step 3: Connect the View and the URL
We need to connect the view to a URL so that it can be accessed by the user. To do this, we will add a URL pattern in the urls.py
file.
from django.urls import path
from . import views
urlpatterns = [
path('form/', views.form_view, name='form_view'),
]
Step 4: Handle the Form Submission
Now that the form is accessible through a URL, we need to handle the form submission in the view.
To handle the form submission, we will check if the request method is POST
. If the request method is POST
, we will retrieve the form data using the request.POST
dictionary.
from django.shortcuts import render, redirect
def form_view(request):
if request.method == 'POST':
name = request.POST['uname'] #uname is the value of name attribute
email = request.POST['uemail'] #uemail is the value of name attribute
return redirect('success')
else:
return render(request, 'form.html')
Step 5: Redirect to a Success Page
Finally, we need to create a success page that will be displayed after the form submission. To do this, we will create another view and URL pattern.
def success_view(request):
return render(request, 'success.html')
#urls.py
urlpatterns = [
path('form/', views.form_view, name='form_view'),
path('success/', views.success_view, name='success_view'),
]
In this blog, we have learned how to get user input from an HTML form in Django. We created a simple HTML form and connected it to a Django view using URL patterns. We also handled the form submission and redirected the user to a success page.
By following these steps, you can easily collect user input in your Django web applications. Remember to always validate and sanitize the user input to prevent security vulnerabilities.
Subscribe to my newsletter
Read articles from shamnad sherief directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
shamnad sherief
shamnad sherief
The future needs more programmers — follow me to learn more about my adventures as a programmer!