The installation
pip install django
Copy the code
Establishing the project
CMD to CD the project directory you are going to create
Django-admin startProject Specifies the project nameCopy the code
To create the APP
CD to your project directory
Python manage.py startApp APP nameCopy the code
Project running
python manage.py runserver
Copy the code
Model
Initial migration (admin, Auth, default model)
python manage.py migrate
Copy the code
Created by the super administrator
python manage.py createsuperuser
Copy the code
Arbitrary APPmodel changes
Generate migration files
python manage.py makemigrations
Copy the code
Migrate files to the database
python manage.py migrate
Copy the code
Urls
Introducing path, include
from django.urls import path, include
Copy the code
The path to the frame
urlpatterns = [
path('admin/', admin.site.urls),
path('test1/', include('test1.urls')),
]
Copy the code
Basic Format of PATH
path('/',views.xx,name='xx'),
Copy the code
Templates
Basic HTML body
<! doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, User - scalable = no, initial - scale = 1.0, the maximum - scale = 1.0, Minimum scale=1.0"> <meta HTTP-equiv =" x-UA-compatible "content=" IE =edge"> <title>Document</title> </head> <body> </body> </html>Copy the code
Template inheritance
{% entends 'xxx.html'%}
Copy the code
Views
Simple HTML page return
Def homepage(request): return render(request, 'xxx.html',contant) def homepage(request): return render(request, 'xxx.html',contantCopy the code
admins
Register, register the model you want to manage in app admin
from django.contrib import admin
from .models import Customer
admin.site.register(Customer)
Copy the code
Forms
Import library
From Django. forms import ModelForm from Django import forms from. Models import Customer // Import the required modelCopy the code
forms.py
class CustomerForm(ModelForm): class Meta: Model =Customer // which model is associated with the fields = '__all__' // the fields to display, __all__ is all display, you want to display what, 'Customername':' Customername', 'Customerlevel':' Customerlevel'}Copy the code
The basic HTML form format
<form action="{% url 'creat' %}" method="post"> C_form. as_p}} //. As_p </form>Copy the code
Pass arguments in view
def creat(request):
c_form = CustomerForm
context={
'c_form':c_form
}
return render(request,'haiqing/html/creat.html', context)
Copy the code