Introduction
Django is an open source web application framework written in Python. Django adopts a web framework constructed by MVC (i.e. model M, view V and controller C). The main purpose of Django is to develop database-driven websites easily and quickly
Django's functions are large and comprehensive, and the one-stop solution it provides allows developers to avoid spending a lot of time on selecting application infrastructure before development. It has many powerful third-party plug-ins that allow us to develop a website more quickly and conveniently.
Install and create projects
pip installation method
pip3 install django
After installing Django, you can use the management tool django-admin, on the dos interface, where you can see all commands and create projects. , service startup, etc.
Create project
A project is a collection of settings related to a Django instance, including database configuration, Django-specific options, and application-specific settings.
Use django-admin startproject project name to create the project
django-admin startproject TestDjango
After execution, the following files will be generated
Directory structure description
-
TestDjango:Project home directory
-
manage.py:A useful command line tool that allows you to interact with this Django project in various ways
-
TestDjango/__init__.py:Empty file, initialization file of Python package
-
TestDjango/settings.py:Configuration of this Django project
-
TestDjango/urls.py:Routing file, matching different url links, calling different view function processing
-
TestDjango/wsgi.py:A portal to a WSGI-compatible web server to run the project
Startup project
Enter the TestDjango directory and enter the following command to start the TestDjango project
python manage.py runserver 127.0.0.1:8080
As shown above, the service startup is completed. The command line cannot be closed after startup
127.0.0.1: Indicates the IP address that can be connected to the server
8080: Port number (if not specified, the port number defaults to 8000)
Browser access 127.0.0.1:8080, the following interface appears to indicate success
Project configuration and application creation
Project configuration
Django is a framework that supports internationalization and localization. If we want to see the Chinese interface, we can modify the following configuration: Modify the TestDjango/settings.py file
# 设置语言代码
LANGUAGE_CODE = 'zh-hans'
Refresh the page just now, and the page will change to Chinese, as shown in the figure below:
Create app
Each application is a Python package and follows the same conventions. Django comes with a tool that can generate the basic directory structure of the application.
The relationship between projects and applications
-
An app is a web application that does something specifically —such as a rating system
-
A project is a collection of configurations and applications used by a website. A project can contain many applications, and applications can be used by many projects
The following example will create a rating application in the same directory as manage.py. This way it can be imported as a top-level module. Please make sure you are in the directory where manage.py is located, and then run this command to create an application
python manage.py startapp score
View configuration
There is a file views.py under the score application, which stores the view interface we wrote.
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("这是一个评分系统!!!")
Create a new urls.py file under the score application. The operation is as follows:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Then you also need to modify the urls.py in the TestDjango directory and add the routing mapping configuration ofscore as follows:
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('score/', include('score.urls')),
path('admin/', admin.site.urls),
]
FinallyWhen we visit the URL http://127.0.0.1:8080/score/, we can access our newly created page, as shown below:
What is include() used for?
The function include() allows references to other URLconfs. Whenever Django encounters an include(), it truncates the portion of the URL that matches this entry and sends the remaining string to the URLconf for further processing.
Django designed include() so that it can be plug-and-play. You should always use include() when including other URL patterns, with admin.site.urls being the only exception.
The Record of Programmers and Investment Life has been renamed Programmer Zhiqiu,the same as the WX official account, welcome to follow! !