Python Development-Web Django Framework Foundation

Posted by gasper000 on Mon, 10 Jan 2022 19:17:54 +0100

Django Foundation


Django is a tripartite Library of python and a web framework (product-level, MTV model) for building a web backend.

Create a project
django-admin startproject myweb
Create an application
python.exe .\ startapp helloapp
Run the project
python.exe .\ runserver

Create a web framework project

django-admin startproject myweb

Frame directory structure

└─myweb     --Outer Catalog
    │        --django Project Interactive Command Tool
    └─myweb             --Project Directory
      --Deployment and Configuration Profiles
            --There is a table in it. URL Route Declaration File
           --Be based on WSGI Of web Server Profile
       --Empty file defining package

About WSGI web Services Gateway Interface

Debug Run web Framework

python.exe .\ runserver

Create a specific application

python.exe .\ startapp helloapp

url routing

views usually write code to handle http requests
Edit file

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

def hello(request):
    return HttpResponse("hello django")

Modify routing file to add routing information

from django.contrib import admin
from django.urls import path
from helloapp.views import hello

urlpatterns = [
    path('hello/', hello),

Routing Subtable

The main route table is under the project, while the secondary route table is generally created by the developer. The main route table represents the route of the current project, and the secondary route table is generally the route of the current project.

Assume there is a connection
In addition to writing them all in the master routing table, we can separate the routing table relationship between the project and the application to make the site structure clearer.

hello can be written in the primary routing table and index in the secondary routing table

Primary Routing Table

Modify master routing table

Introduce include

The following path('hello/', include('helloapp.urls')) indicates that whenever accessed by this path in hello, the routing information in the secondary routing table is used

Secondary Routing Table

Add a new routing file under the application file
Write down the information in this routing table and access index is handled by newfun

from django.urls import path
from helloapp.views import newfun,newfun2,newfun3

urlpatterns = [
    path('index/', newfun),

Django operates on sqlite databases

sqlite does not have a stand-alone database service process, and data operations are invoked as libraries by direct providers. Django can be used directly without having to set up a data service first.

The project creates a dB when it is created. SQLite3 file, this is an empty file, we need to create the related library tables by ourselves

python migrate

Connect through database tools

ORM (object relational mapping) concept

In django, database operations are essentially done through objects of type Model, which do not use sql statements but use class methods.

Define database tables

Definition of database tables, generally models applied. Inside PY

Refer to the official documentation for field types:
Write Fields

from django.db import models

# Create your models here.
from django.db import models
from django.contrib import admin
# Create your models here.

class Customer(models.Model):
	name = models.CharField(max_length=200)
	phone = models.CharField(max_length=200)
	address = models.CharField(max_length=200) # Add to django admin management

Adding configurations to the project's configuration file means we have such an application

Then execute

python.exe .\ makemigrations common

Migration\0001_under Generation Application Initial. Py file

python.exe .\ migrate

Write to database

Each table under django needs to have a primary key. If not configured, django automatically generates a primary key as id

If you modify the generated table file, you need to rerun makemigrations and import the database migrate

Django Admin Manages Data

Create users through createsuperuser

python.exe .\ createsuperuser

Log in through django's default admin page

If admin configuration management is not introduced when creating tables
The admin in the application table is required. Py configuration management

from django.contrib import admin

from .models import Customer

Topics: Python Django