current position:Home>Construction of the first Django project

Construction of the first Django project

2022-02-01 11:35:07 Nuggets

This is my participation 11 The fourth of the yuegengwen challenge 25 God , Check out the activity details :2021 One last more challenge

Django Is currently the best based on Python Of Web One of the application development platforms , This article briefly introduces it and creates an empty Django engineering .

Front and rear ends

  • Web The purpose of development can be said to provide users with a service scenario that can obtain information or interact , This involves several important modules : Display module 、 Business processing module and data storage module .
The server
Data interaction
Resolve request
Generate html page
Business processing
  • The big framework is that users make service requests to the server , The server receives a request to extract data from the database , Send it to the processing module and get the content required by the user after processing , Then the content is displayed in front of the user through the display module .
  • In the process , Current data processing 、 The part of business processing is called the back end , Show users the content and design and users 、 The part of the interaction logic of the back end is called the front end .
  • In order to decouple business processing and interface display , After years of development experience , The current best practice is to separate the front and rear ends , Make the front-end personnel and the back-end personnel agree on the interface for interaction , After that, the front end only needs to do a good job of the interface , The back end only needs to do business logic processing .


brief introduction

Django Is an open source Web Application framework , from Python It's written in , Its main purpose is to simplify 、 Fast development of database driven websites . It emphasizes code reuse , Multiple components can be easily used to “ plug-in unit ” Form serves the whole framework ,Django There are many powerful third-party plug-ins , You can even easily develop your own toolkit .


Django The development team is extremely diligent , The version plan is very compact , Iterations are frequent

Release Series Latest Release End of mainstream support1 End of extended support2
4.2 LTS April 2023 December 2023 April 2026
4.1 August 2022 April 2023 December 2023
4.0 December 2021 August 2022 April 2023
3.2 LTS April 2021 December 2021 April 2024
3.1 3.1.5 April 2021 December 2021
3.0 3.0.11 August, 2020 April, 2021
2.2 LTS 2.2.17 December 2, 2019 April 2022
2.1 2.1.15 April 1, 2019 December 2, 2019
2.0 2.0.13 August 1, 2018 April 1, 2019
1.11 LTS 3 1.11.29 December 2, 2017 April 1, 2020
1.10 1.10.8 April 4, 2017 December 2, 2017
1.9 1.9.13 August 1, 2016 April 4, 2017
1.8 LTS 1.8.19 December 1, 2015 April 1, 2018
1.7 1.7.11 April 1, 2015 December 1, 2015
1.6 1.6.11 September 2, 2014 April 1, 2015
1.5 1.5.12 November 6, 2013 September 2, 2014
1.4 LTS 1.4.22 February 26, 2013 October 1, 2015
1.3 1.3.7 March 23, 2012 February 26, 2013

  • Django be based on Python Development , And based on Python Environment is running , Different Django Version pair Python Versions have different requirements :
Django edition Python edition
1.8 2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5
1.9, 1.10 2.7, 3.4, 3.5
1.11 2.7,3.4,3.5,3.6,3.7 (1.11.17 add to )
2.0 3.4,3.5,3.6,3.7
2.1,2.2 3.5, 3.6, 3.7
3.0 3.6, 3.7, 3.8
3.1 3.6, 3.7, 3.8 or later


  • Determine what you need Django edition , At present (2021 year 1 month 23 Japan ) Abreast of the times Django Version is 3.1.5, my python edition 3.8.3
  • Use pip install :
pip install Django
 Copy code 

here pip The default installation is 3.0.5 edition

  • If you need the latest version, you can specify :
pip install Django==3.1.5
 Copy code 
pip show django
 --> Name: Django
Version: 3.0.5
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Author: Django Software Foundation
Author-email: [email protected]
License: BSD
Location: d:\program_files\anaconda3\lib\site-packages
Requires: pytz, asgiref, sqlparse
 Copy code 


python -m django --version
 --> 3.0.5
 Copy code 

Project creation

  • Find a place to store Django Project folder , Open the terminal here , Enter command :

Suppose we want to create a file called vvd_django The engineering of :

django-admin startproject vvd_django
 Copy code 
  • The directory structure of the current project :
tree /F  # windows
tree -a  # Linux
 --> └─vvd_django
 Copy code 
  • The use of these directories and files is :
    • The outermost vvd_django/ The root directory is just a container for your project , The root directory name is right Django No impact , You can rename it to any name you like .
    • One lets you manage... In various ways Django Command line tools for projects . You can read django-admin and Get all The details of the .
    • It's on the inside vvd_django/ The catalog contains your projects , It's a pure Python package . Its name is what you need to use when you reference anything inside it Python Package name . ( such as vvd_django.urls).
    • vvd_django/ An empty file , tell Python This directory should be considered a Python package . If you are Python beginner , Read... In the official document More about packages .
    • vvd_django/ The configuration file for the project . If you want to know how this file works , Please check out Django To configure Understand the details .
    • vvd_django/ Project URL Statement , Like your website “ Catalog ”. read URL Scheduler Documentation to get more about URL The content of .
    • vvd_django/ As your project runs in ASGI Compatible Web Entry on the server . read How to use WSGI Deployment Learn more about .
    • vvd_django/ As your project runs in WSGI Compatible Web Entry on the server . read How to use WSGI Deployment Learn more about .
    • Synchronize database files :

Django The default database is db.sqlite3

cd vvd_django
python migrate
 --> Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
 Copy code 
  • start-up Django Server , Check our current project status :
python runserver
 --> Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 23, 2021 - 15:42:03
Django version 3.0.5, using settings 'vvd_django.settings'
Starting development server at
Quit the server with CTRL-BREAK.
 Copy code 

The appearance of a small rocket shows our first Django The project is in normal operation .

Write the first view

New application

  • Run in the project root directory terminal :
python startapp hello_world
 Copy code 
  • Current project file structure
│  db.sqlite3
│  │
│  │
│  │
│  │
│  │
│  │
│  └─migrations
 Copy code 

New view

open hello_world/, Put these down here Python Code in :

from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the vvd hello_world index.")
 Copy code 

This is a Django The simplest view in . If you want to see the effect , We need to put a URL Map to it

application url mapping

  • stay hello_world Create a new one in the catalog file , And enter the code :
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
 Copy code 

root url mapping

  • At the root URLconf The file specifies the hello_world.urls modular . stay vvd_django/ Of documents urlpatterns Insert one into the list include(), as follows :
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('hello_world/', include('hello_world.urls')),

 Copy code 

function include() It is allowed to quote other URLconfs, The idea is to make it plug and play . whenever Django encounter include() when , It will truncate the URL Part of , And send the rest of the string to URLconf For further processing . They can be placed in any path , Make the application work properly .

Operation service

python runserver
 Copy code 

To access the application

  • It shows that our application has been successfully called

Reference material

copyright notice
author[Nuggets],Please bring the original link to reprint, thank you.

Random recommended