Django is a popular Python web framework that provides an efficient and powerful way to develop web applications. This article will introduce the basic knowledge of Django from entry to master, and provide specific code examples.
Before using Django, you need to make sure that Python is installed on your computer. Then, you can install Django using the following command:
pip install Django
To create a new Django project, you can use the following command:
django-admin startproject <projectname>
For example, to create a project named "mysite", execute the following command:
django-admin startproject mysite
To run the Django server, enter the project file folder and use the following command:
python manage.py runserver
By default, the server will run on your local machine and listen on port 8000.
In a Django project, an application is the basic component of a web application. A new application can be created using the following command:
python manage.py startapp <appname>
For example, to create an application named "blog", execute the following command:
python manage.py startapp blog
Django views are code in a web application that handle requests from users and return responses. They can be written as functions or classes. The following is a simple Django view example:
from django.http import HttpResponse def hello(request): return HttpResponse("Hello, world.")
In this example, the view function "hello" receives an HTTP request and returns an HTTP response containing "Hello, world."
Django's template is an HTML file that can use Django's template language to render dynamic content. Here is a simple template example:
<!doctype html> <html> <head> <title>{{ title }}</title> </head> <body> <h1>{{ heading }}</h1> <p>{{ content }}</p> </body> </html>
In this example, "title", "heading" and "content" are variables and their values can be set in the view.
In Django, a URL is an address used in a web application to which a view function will respond. You can use the following code to define a URL for a view function:
from django.urls import path from . import views urlpatterns = [ path('hello/', views.hello), ]
In this example, the URL "/hello/" will point to the view function "hello".
Django provides an ORM (Object Relational Mapping) that supports a variety of databases. With an ORM, you can use Python code to interact with the database without writing raw SQL queries. Here is a simple example of using an ORM to create a database table:
from django.db import models class BlogPost(models.Model): title = models.CharField(max_length=100) content = models.TextField() pub_date = models.DateTimeField(auto_now_add=True)
In this example, "BlogPost" is a Django model that will be mapped to a table in the database. It defines three fields: "title", "content" and "pub_date".
Once the model is defined, you can create the relevant tables in the database using the following command:
python manage.py makemigrations python manage.py migrate
First command A migration script will be created and the second command will apply the script and update the database.
Django comes with a powerful management site that can be used to manage data in web applications. Create an administrator account with the following command:
python manage.py createsuperuser
You can then log in to the admin site by accessing "/admin/" in a web browser.
To sum up, Django provides an efficient and powerful way to develop web applications. This article covers the basics of Django, including how to create a Django project, write Django views and templates, define Django URLs, connect to a database, run Django data migrations, and manage a site using Django. By understanding and practicing these basics, you can go from getting started with Django to becoming proficient.
The above is the detailed content of Understanding the Django framework: from entry to mastery. For more information, please follow other related articles on the PHP Chinese website!