19.3. Forms in Django: Creating a Django Project

Django is one of the most popular frameworks for developing web applications with Python. It offers a robust set of tools and features that make it easy to build complex, scalable systems. In this chapter, we'll explore creating forms in Django and how they can be used to collect and process user information.

Creating a Django project

To start working with Django, we first need to create a new Django project. This can be done using the command 'django-admin startproject' followed by the project name. For example, to create a project called 'mysite' you would use the command:


$ django-admin startproject mysite

This command creates a new folder named after the project and generates a directory structure that includes the necessary configuration files for the Django project. The basic structure of a Django project includes the following:

  • manage.py: This is a command-line utility that lets you interact with the Django project in a variety of ways.
  • mysite/: This directory is the project package. It is the container for your project and can be renamed to anything you like.
  • mysite/settings.py: This file contains all the settings for the Django project.
  • mysite/urls.py: This file is used to define the URLs for this Django project.
  • mysite/wsgi.py: This file is the entry point for WSGI-compatible web servers to serve your project.

Forms in Django

Forms are an essential part of any web application. They allow users to provide information that can be processed by the server. Django offers a powerful and flexible way to work with forms.

To create a form in Django, we first need to define a form class. This class inherits from forms.Form and defines the fields that the form will contain. Each field is represented by an instance of a Field class, such as CharField for text fields, EmailField for email fields, etc.


from django import forms

class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

Once the form is defined, it can be used in a view to render the HTML form and to process the form data. Django provides a number of conveniences for handling forms, including data validation and HTML generation.

To use the form in a view, we first import the form, create an instance of the form, pass the form to the context, and finally render the form in the template.


from django.shortcuts import render
from .forms import ContactForm

def contact(request):
    form = ContactForm()
    return render(request, 'contact.html', {'form': form})

In the template, we use the 'form' template tag to render the form fields. Django will generate the necessary HTML for each form field.


<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>

In summary, forms are an essential part of Django that allow us to collect and process user information efficiently and securely. With practice and experience, you'll find that Django offers many powerful tools and features to make working with forms simple and enjoyable.

Now answer the exercise about the content:

What is the role of the 'mysite/urls.py' file in a Django project?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Forms in Django: Creating a Django Application 117

Next page of the Free Ebook:

Forms in Django: Creating a Django Application

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks