Home >Backend Development >Python Tutorial >Learn how to build efficient web applications in Django from scratch
Django installation tutorial: Build an efficient Web application from scratch, specific code examples are required
Introduction:
Django is an efficient Web written in Python Application development framework. It provides a way to quickly build stable, secure and scalable web applications. This article will introduce in detail how to install and configure Django from scratch, and provide specific code examples to help beginners get started smoothly.
1. Install Python and pip
Django is developed based on Python, so you need to install Python on your computer first. You can download the latest version of Python from the official website (https://www.python.org/downloads/) and follow the installation wizard to complete the installation.
After installing Python, you need to install pip, which is Python's package management tool. Enter the following command on the command line:
$ python -m ensurepip --upgrade
$ python -m pip install --upgrade pip
2. Install Django
After pip is installed, we can use it to install Django. Enter the following command on the command line:
$ pip install django
3. Create a Django project
After installing Django, we can start creating a new Django project. Enter the following command at the command line:
$ django-admin startproject myproject
This will create a folder named "myproject" in the current directory and generate the basic structure of the Django project in it.
4. Run the Django development server
Enter the project folder "myproject" and enter the following command in the command line:
$ python manage.py runserver
This will start the Django development server and listen locally by default 8000 port. Enter "http://localhost:8000" in your browser and you will see Django's default welcome page.
5. Create a Django application
In addition to the structure of the project itself, we can also create applications in the Django project. Enter the following command at the command line:
$ python manage.py startapp myapp
This will create an application named "myapp" in the project and generate the basic structure of the application within it.
6. Create a model
Model is a class used in Django to define the database structure. In the "models.py" file of the "myapp" application, we can define our models. The following is the code for an example model:
from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) publication_date = models.DateField() def __str__(self): return self.title
This model defines a class named "Book", which has three attributes: title, author, and publication_date. We can also specify what is displayed when printing the object in the console by overriding the __str__() method.
7. Database migration
After defining the model, we need to tell Django that our database structure has changed. Enter the following command on the command line:
$ python manage.py makemigrations
This will generate a series of database migration files to record database changes. Then enter the following command:
$ python manage.py migrate
This will perform the actual change operation of the database based on the migration file.
8. Create views and URLs
Views are functions used in Django to process user requests. In the "myapp" application, we can define our views in the "views.py" file. The following is the code for a sample view:
from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello, world!")
This view function receives a request object and returns a response object containing the text "Hello, world!"
In order to make our view accessible, we also need to add the corresponding URL configuration in the "urls.py" file of the "myproject" project. Here is an example URL configuration code:
from django.urls import path from myapp.views import index urlpatterns = [ path('', index, name='index'), ]
This will map the empty path to the "index" view function we defined earlier.
9. Run the Django development server
After completing the above steps, we can run the Django development server again to view our application. Enter the following command in the command line:
$ python manage.py runserver
Then enter "http://localhost:8000" in the browser, you will see the "Hello, world!" text we defined earlier.
Conclusion:
This article introduces the installation and configuration process of Django and provides some specific code examples. I hope that through this tutorial, beginners can successfully build their own Django project and understand the basic usage of Django. Of course, in addition to what is mentioned in this article, Django has many other powerful functions and tools that require further learning and practice.
The above is the detailed content of Learn how to build efficient web applications in Django from scratch. For more information, please follow other related articles on the PHP Chinese website!