The latest Django framework is version 5.0.4, and it was released on April 28, 2024. To install it, you can use the following command for your operating system:
For Linux / macOS:
python -m pip install Django==5.0.4
For Windows:
py -m pip install Django==5.0.4
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It is designed to help developers take applications from concept to completion as quickly as possible, while also being reassuringly secure and exceedingly scalable.
In Django, a web application is made up of models, views, and templates. Models are used to define the structure of the data in your application. Views are used to handle requests and return responses. Templates are used to define the structure of the HTML pages that are returned to the user.
Django also provides a way to navigate around the different pages in a website. When a user requests a URL, Django decides which view it will send it to. This is done in a file called urls.py.
Here is an example of a minimal view function in Django:
# filename: views.py (Django view functions) from django.http import HttpResponse def index(request): # Get an HttpRequest - the request parameter # perform operations using information from the request. # Return HttpResponse return HttpResponse('Hello from Django!')
This function receives an HttpRequest object as a parameter and returns an HttpResponse object. In this case, the function returns a hard-coded string.
Here is an example of a view function that uses a model to retrieve data and render it in a template:
## filename: views.py from django.shortcuts import render from .models import Team def index(request): list_teams = Team.objects.filter(team_level__exact="U09") context = {'youngest_teams': list_teams} return render(request, '/best/index.html', context)
This function uses the render() function to create the HttpResponse that is sent back to the browser. The render() function is a shortcut that creates an HTML file by combining a specified HTML template and some data to insert in the template. In this case, the data is a list of teams that have a team level of "U09".The templates of an application are located in a folder named templates. Templates use standard HTML to describe the layout, but use Django tags to add logic. For example, the following template displays the first name of a user:
<h1>My Homepage</h1> <p>My name is {{ youngest_teams }}.</p>