formcarry.

FORM QUESTIONS

How to get data from HTML form in Django?

In Django, you can retrieve data from an HTML form using the request object. Here is an example of how to do it:

Define a function in Django to handle form

In your views.py, define a view function that will handle the form submission.

VS Code

Copy Code

1def my_view(request):
2    if request.method == 'POST':
3        # retrieve form data
4        form_data = request.POST
5
6        # retrieve individual field values
7        name = form_data['name']
8        email = form_data['email']
9        message = form_data['message']
10
11        # print the values
12        print("Name:", name)
13        print("Email:", email)
14        print("Message:", message)
15
16        # redirect to a new URL
17        return redirect('success_url')
18
19    else:
20        # display the form
21        return render(request, 'template.html')
Copied example to clipboard 👏

Create your HTML form

In your template.html, create the HTML form:

VS Code

Copy Code

1<form method="post" action="{% url 'form_page' %}">
2  <label for="name">Name:</label>
3  <input type="text" id="name" name="name">
4
5  <label for="email">Email:</label>
6  <input type="email" id="email" name="email">
7
8  <label for="message">Message:</label>
9  <textarea id="message" name="message"></textarea>
10  
11  <input type="submit" value="Submit">
12</form>
Copied example to clipboard 👏

or you can use our free contact form generator to get a beautiful contact form code

That's all you need to get data from your HTML form in Django.

Update urls.py

In your urls.py, create a URL pattern for the view:

VS Code

Copy Code

1from django.urls import path
2from .views import my_view
3
4urlpatterns = [
5    path('form-page/', my_view, name='form_page'),
6]
Copied example to clipboard 👏

Summary

In this mini article we've showed you how to process HTML form data in Django as simple as possible, hope it helps!


Looking for to save the data of your HTML form?

The basic functionality of the code above may not be enough for you, fortunately we have a solution. If you want to have built-in spam protection system, file uploads, email notifications and integrations take a look at our product. Formcarry provides an hassle free way to process HTML forms in seconds just by changing the form like this;

VS Code

Copy Code

1- <form method="post" action="{% url 'form_page' %}">
2+ <form method="post" action="https://formcarry.com/s/YOUR_UNIQUE_FORM_ENDPOINT">
Copied example to clipboard 👏

Here's a detailed article on How to make a working HTML contact form with formcarry

:)
:)
:)
:)

LET'S GET STARTED!

Hassle Free Forms

Create an account and start collecting submissions

for your HTML forms, it only takes 2 minutes to setup your form

formcarry dashboard.
Blur
Your email address|
Copied example to clipboard 👏