Forms in Django: URLs and views in Django

Capítulo 121

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Django is a high-end web development framework, written in Python, that encourages fast, clean development with pragmatic design. One of its main features is the ability to create forms quickly and efficiently. Let's explore section 19.8 of our e-book course, which covers creating forms in Django, as well as configuring URLs and views.

Forms in Django

Django offers a powerful and flexible way to work with forms. In Django, forms are treated as objects, which allows for a great deal of customization and reuse. Creating a form in Django involves defining a class that inherits from forms.Form or forms.ModelForm. This class defines the form fields and can include form validation, custom widgets, and business logic.

For example, a simple contact form might be defined as follows:

from django import forms

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

In this example, the form contains three fields: a text field for the name, an email field, and a text field for the message. Each field is defined by a specific form field type, which determines the type of validation that is applied and the widget that is used to render the field.

URLs in Django

URLs in Django are defined in a URL file, often called urls.py. This file defines a mapping between URLs and views. Each URL is associated with a view, which is a Python function that takes an HttpRequest object and returns an HttpResponse object.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

For example, the URL for the contact page can be defined as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('contact/', views.contact, name='contact'),
]

In this example, the URL /contact/ is mapped to the view contact. The view is responsible for processing the request and returning the response. The name 'contact' is used to reference the URL in other parts of the code, such as in templates.

Views in Django

Views in Django are Python functions that take an HttpRequest object and return an HttpResponse object. The view is responsible for processing the request and preparing the response. This may involve reading data from a database, rendering a template, or performing some other type of processing.

For example, the view for the contact page can be defined as follows:

from django.shortcuts import render
from .forms import ContactForm

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            # process the data in form.cleaned_data
            pass
    else:
        form = ContactForm()

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

In this example, the view checks whether the request is a POST. If so, it creates an instance of the form with the request data. If the form is valid, it processes the data. If the request is not a POST, it creates an empty instance of the form. In both cases, it renders the 'contact.html' template with the form.

In short, forms in Django are powerful and flexible, and configuring URLs and views is simple and straightforward. With Django, you can create robust, scalable web applications easily and efficiently.

Now answer the exercise about the content:

What is Django and what are its main features?

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

You missed! Try again.

Django is a web development framework, written in Python, not a programming language or a database. It is designed to encourage fast, clean development with a pragmatic approach. One of its main features, as mentioned, is the ability to create forms quickly and efficiently. This aligns with Option 2, which correctly identifies Django's nature and main feature of handling forms.

Next chapter

Forms in Django: Templates in Django

Arrow Right Icon
Free Ebook cover System creation course with Python and Django complete
69%

System creation course with Python and Django complete

New course

176 chapters

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