python web framework django(1)

1. MVC and MTV mode

1.1 MVC mode

Divide the application into the following three layers:
1. Model (M)
2. Controller (C)
3. View (V)

1.2 MTV model

Django's model
1. Model (model): responsible for business objects and database objects (ORM)
2. Template (template): responsible for displaying pages
3. View (view): responsible for business logic, and call Model and Template when appropriate
**Django also has a url distributor, which is responsible for matching the url to the view

2. Django process and command tools

1. Install django
pip install django

2. Create a django project
django-admin.py startproject mysite

3. Create a blog application
python manage.py startapp blog

4. Start the service
python manage.py runserver 8080

5. Generate the script
python manage.py makemigrations to synchronize the database

6. Synchronize the database
python manage.py migrate

7. Create super administrator
python manage.py createsuperuser

Other:
1. Clear the database
python manage.py flush

2. View the detailed information of the command
django-admin.py help startapp

3. Start the interactive interface,
you can call the API
python manage.py shell in models.py in the current project

3. Practice

3.1 Exercise 1-Submit data and display

1. Create an html page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>创建个人信息</h1>

<form action="/userInfor/" method="post">

    <p>姓名<input type="text" name="username"></p>
    <p>性别<input type="text" name="sex"></p>
    <p>邮箱<input type="text" name="email"></p>
    <p><input type="submit" value="submit"></p>

</form>

<hr>

<h1>信息展示</h1>

<table border="1">

    <tr>
        <td>姓名</td>
        <td>性别</td>
        <td>邮箱</td>
    </tr>
    {% for i in info_list %}

        <tr>
            <td>{{ i.username }}</td>
            <td>{{ i.sex }}</td>
            <td>{{ i.email }}</td>
        </tr>

    {% endfor %}

</table>

</body>
</html>

2. Modify the template location-settings

Insert picture description here
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,"templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

3. Add view

from django.shortcuts import render

# Create your views here.
info_list=[]

def userInfor(req):

    if req.method=="POST":
        username=req.POST.get("username",None)
        sex=req.POST.get("sex",None)
        email=req.POST.get("email",None)

        info={"username":username,"sex":sex,"email":email}
        info_list.append(info)

    return render(req,"userInfor.html",{"info_list":info_list})


4. Mapping url--urls

from blog import views


urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'userInfor/', views.userInfor)
]

3.2 Exercise 2-Submit data and display (database)

1.userinfo.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>test</title>

</head>
<body>
<h1>创建个人信息</h1>

<form action="/userInfor/" method="post">
	<p>name<input type="text" name="username"></p>
	<p>sex<input type="text" name="sex"></p>
	<p>address<input type="text" name="address"></p>
	<p><input type="submit" value="submit"></p>

</form>

<hr>
<h1>信息展示</h1>
<table border="1">
	<tr>
		<td>name</td>
		<td>sex</td>
		<td>address</td>
	</tr>
	{% for i in info_list %}
	<tr>
		<td>{{ i.username}}</td>
		<td>{{ i.sex}}</td>
		<td>{{ i.address}}</td>
	</tr>
	{% endfor %}

</table>

</body>

</html>

2.views

from django.shortcuts import render
from blog import models

def userInfor(req):

    if req.method=="POST":
        print(req.POST)
        name=req.POST.get("username",None)
        sex=req.POST.get("sex",None)
        address=req.POST.get("address",None)

        
        #插入数据方式1
#         info={"username":name,"sex":sex,"address":address}
#         models.UserInfor.objects.create(info)
        #插入数据方式2
        models.UserInfor.objects.create(
            username=name,
            sex=sex,
            address=address
            )
        info_list = models.UserInfor.objects.all()

        return render(req,"userInfor.html",{"info_list":info_list})
    return render(req,"userInfor.html")


3.setthings

INSTALLED_APPS = [
    ...
    'blog'
]

4.models

from django.db import models

# Create your models here.

class UserInfor(models.Model):
    username = models.CharField(max_length=64)
    sex = models.CharField(max_length=64)
    address = models.CharField(max_length=64)