The system creation course with Python and Django is an unmissable opportunity for anyone who wants to go deeper into the development of web applications. In this chapter, we'll cover a crucial aspect of Django: creating and manipulating templates. In addition, we'll explore how views work in Django and how we can use them to make our applications more dynamic and interactive.
18.4 Templates in Django
In Django, templates are the layer responsible for presenting data to the user. They are written in a language called Django Template Language (DTL), which is a markup language similar to HTML, but with some useful additions that allow us to insert dynamic data into our pages.
Templates are stored in .html files and can be organized in directories however you like. However, it is common to place all templates related to a given application in a directory called 'templates' within the application directory.
To create a template, simply create a new .html file and start writing your DTL code. You can use all the normal HTML tags, but you also have the option of using special DTL tags, which are enclosed in double braces, such as {{ }} and {% %}. These tags allow you to insert variables, loops, conditionals and other control structures into your templates.
For example, if you want to display the username on a page, you can pass a variable called 'username' to the template and then use the {{ username }} tag to insert the value of that variable into your page.
Views in Django
Views, on the other hand, are the layer responsible for the business logic of your applications. They are Python functions or classes that take an HTTP request, process that request in some way, and then return an HTTP response.
Each view in Django maps to one or more URLs, which means that when a user visits a certain URL on your site, Django will execute the corresponding view and return the result to the user.
Views can do many different things, depending on what your application needs. They can render templates, return data in JSON format, redirect the user to another page, among other things.
To create a view, you need to define a Python function or class in your views.py file. This function or class must receive at least one argument, which is an instance of the HttpRequest class. It must then return an instance of the HttpResponse class or one of its subclasses.
For example, here's how you could define a simple view that renders a template:
def my_view(request): return render(request, 'my_template.html')
Django provides many useful tools and abstractions to make creating views easier, such as the render() function, which renders a template and returns the result as an HTTP response. In addition, Django also supports class-based views, which can be more reusable and modular than function-based views.
In short, templates and views are two fundamental pieces of Django that work together to create dynamic and interactive web applications. With a solid understanding of these concepts, you'll be well prepared to continue your learning journey into the world of web development with Python and Django.