Django is a high-end web development framework, written in Python, that promotes fast, clean development with a pragmatic design. One of Django's most powerful features is the template system. Django's templating system is a way to separate HTML from Python code, making it easier to see what's going on and making page design simpler.

To get started with Django's templates, you need to understand how Django finds and loads templates. By default, Django looks for templates in a directory called 'templates' in every installed application. So, if you have an application called 'blog', Django will look for templates in the 'blog/templates' directory.

To use a template in a view, you first need to load the template using the 'get_template()' function of the 'django.template.loader' module. This function takes the template name as an argument and returns a template object. You can then call the 'render()' method of this template object, passing it a context. The context is a dictionary that maps variable names to their values.

For example, if you have a template called 'blog/post_detail.html', you could load and render this template like this:


from django.template.loader import get_template
from django.http import HttpResponse

def post_detail(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    template = get_template('blog/post_detail.html')
    context = {'post': post}
    return HttpResponse(template.render(context, request))

This code loads the 'blog/post_detail.html' template, creates a context that contains the post we want to display, and then renders the template with that context. The result is a string of HTML which is then sent as an HTTP response.

Django templates use their own language, called the Django templating language. This language includes tags, which are special instructions you can use to add logic to your HTML. For example, the 'if' tag allows you to add conditions to your template. You can use the 'if' tag to display different content depending on the value of a variable.

In addition to tags, Django's templating language also includes filters, which are functions you can apply to variables. For example, the 'date' filter allows you to format dates and times. You can use the 'date' filter to display the publication date of a post in whatever format you like.

Forms are another crucial part of web development with Django. Django comes with a powerful forms library that makes it easy to deal with rendering HTML forms and validating form data. To create a form with Django, you define a class that inherits from 'django.forms.Form' and define the form's fields as attributes of the class. Each field is an instance of a field class, such as 'CharField' for text fields or 'IntegerField' for integers.

For example, to create a comment form, you could define the following class:


from django import forms

class CommentForm(forms.Form):
    name = forms.CharField(max_length=100)
    comment = forms.CharField(widget=forms.Textarea)

This class defines a form with two fields, 'name' and 'comment'. The 'name' field is a text field that accepts up to 100 characters. The 'comment' field is a text area, as indicated by the 'Textarea' widget.

To render this form in a template, you can simply pass it to the context and use it in an HTML 'form' tag. Django will take care of rendering the fields and generating the appropriate HTML 'input' tags.

For example, you could render the comment form like this:


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

This code renders the comment form as a series of paragraphs, each containing a form field. The 'as_p' method of the form object generates the HTML for each field, including the 'label' and 'input' tags and any error messages.

In summary, Django provides a powerful infrastructure for handling templates and forms, making it easy to create robust and maintainable web applications.

Now answer the exercise about the content:

What is the role of Django's templating system?

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

You missed! Try again.

Article image Forms in Django: Forms in Django 123

Next page of the Free Ebook:

Forms in Django: Forms in Django

Estimated reading time: 3 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