• Python Django 之 简单入门


    一、下载Django并安装

    1、下载Django

    2、安装

    二、新建Django project

    1、使用django-admin新建mysite 项目

    django-admin startproject mysite

    在C:Usershopepython目录新建Django项目

    2、查看mysite里面的内容

    1)外层mysite

    2)内存mysite

    settings(设置)、url(路由分配)、wsgi(web服务器)

    三、新建APP

    1、使用python manage.py startapp 新建app

    python manage.py startapp blog

    2、查看新建app

    3、查看app内的文件

    四、简单的Djagno使用一

    注意:

    1)request接收前端数据

    2)HttpResponse的使用,向前端发送数据

    1、url

    from django.contrib import admin
    from django.urls import path
    from blog import views
    urlpatterns = [
    path('admin/', admin.site.urls),
    path('cur_time/',views.cur_time),

    2、views

    from django.shortcuts import render,HttpResponse

    # Create your views here.
    def cur_time(request):
    return HttpResponse("<h1>ok</h1>")

    3、templates

    暂无

    4、启动

    python manage.py runserver 8080

    五、简单的Djagno使用二--后端向前端发送数据

    注意:

    1)render的使用:render封装了HttpResponse

    2)render后端向前端,发送数据的格式render(request,"cur_time.html",{"abc":times})

    3)前端接收后端数据格式{{  }}

    1、url

    from django.contrib import admin
    from django.urls import path
    from blog import views
    urlpatterns = [
    path('admin/', admin.site.urls),
    path('cur_time/',views.cur_time),

    2、views

    from django.shortcuts import render,HttpResponse
    import datetime
    # Create your views here.
    def cur_time(request):
    times=datetime.datetime.now()
    return render(request,"cur_time.html",{"abc":times})

    3、templates

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <h1>当前时间:{{ abc }}</h1>
    </body>
    </html>

    4、启动

    不用重新启动,Django会自动刷新

    六、简单的Djagno使用三--前端向后端发送数据

    注意:

    1)action

    2)method

    3)req: req.POST.get

    1、url

    from django.contrib import admin
    from django.urls import path
    from blog import views
    urlpatterns = [
        path('admin/', admin.site.urls),
    path('cur_time/',views.cur_time),
        path('userInfo/',views.userInfo),
    ]

    2、views

    from django.shortcuts import render,HttpResponse
    import datetime
    # Create your views here.
    def userInfo(req):
    if req.method=="POST":
    username=req.POST.get("username",None)
    sex=req.POST.get("sex", None)
    email=req.POST.get("email", None)

    print(username)
    print(sex)
    print(email)
    return render(req,"index.html")

    3、templates

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <form action="/userInfo/" 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" name="submit"></p>

    </form>
    </body>
    </html>

    4、启动填写提交的信息

    5、Forbidden(跨站请求攻击)与处理

    1)点击提交,出现Forbidden (403) 跨站请求攻击

    2)处理Forbidden (403)

    注释掉setting中的MIDDLEWARE的   'django.middleware.csrf.CsrfViewMiddleware',

    3)再次点击提交

    4)后台输出对应的数据

    七、简单的Djagno使用四--前后端数据交互

    1、url

    from django.contrib import admin
    from django.urls import path
    from blog import views
    urlpatterns = [
    path('admin/', admin.site.urls),
    path('cur_time/',views.cur_time),
    path('userInfo/',views.userInfo),
    ]

    2、views

    from django.shortcuts import render,HttpResponse
    import datetime
    # Create your views here.
    user_list=[]
    def userInfo(req):
    if req.method=="POST":
    username=req.POST.get("username",None)
    sex=req.POST.get("sex", None)
    email=req.POST.get("email", None)

    user={"username":username,"sex":sex,"email":email}
    user_list.append(user)
    return render(req,"index.html",{"user_list":user_list})

    3、templates

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <form action="/userInfo/" 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" name="submit"></p>

    </form>
    <hr>
    <h1>
    数据展示
    </h1>
    <tale border="10px">
    <tr>
    <td>姓名</td>
    <td>性别</td>
    <td>邮箱</td>
    </tr>
    <br/>
    <tr>
    {% for i in user_list %}
    <td>{{ i.username }}</td>
    <td>{{ i.sex }}</td>
    <td>{{ i.email }}</td>
    <br/>
    {% endfor %}
    </tr>

    </tale>
    </body>
    </html>

    4、启动展示

    1)前台输入新数据到后端

    2)前台展示后端发来的数据

    八、简单的Djagno使用四--从数据库中存取数据

    注意:

    1)setting查看数据库配置

    2)models写法

    1、url

    from django.contrib import admin
    from django.urls import path
    from blog import views
    urlpatterns = [
    path('admin/', admin.site.urls),
    #path('cur_time/',views.cur_time),
    path('userInfo/',views.userInfo),
    ]

    2、views

    from django.shortcuts import render,HttpResponse
    import datetime
    from blog import models

    def userInfo(req):
    if req.method=="POST":
    u=req.POST.get("username",None)
    s=req.POST.get("sex", None)
    e=req.POST.get("email", None)
    models.UserInfo.objects.create(
    username=u,
    sex=s,
    email=e,
    )
    user_list=models.UserInfo.objects.all()

    return render(req,"index.html",{"user_list":user_list})

    3、 templates

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    <form action="/userInfo/" 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" name="submit"></p>

    </form>
    <hr>
    <h1>
    数据展示
    </h1>
    <table border="1px">
    <tr>
    <td>姓名</td>
    <td>性别</td>
    <td>邮箱</td>
    </tr>
    <br/>
    <tr>
    {% for i in user_list %}
    <td>{{ i.username }}</td>
    <td>{{ i.sex }}</td>
    <td>{{ i.email }}</td>

    {% endfor %}
    </tr>
    <br/>
    </table>
    </body>
    </html>

    4、models

    from django.db import models

    # Create your models here.
    class UserInfo(models.Model):
    username=models.CharField(max_length=60)
    sex = models.CharField(max_length=60)
    email = models.CharField(max_length=60)

    5、database

    python manage.py makemigrations

    python manage.py migrate

    6、重启后数据不再丢失

    
    
    
  • 相关阅读:
    nyoj163 Phone List
    hdu1251统计难题
    hdu1754 I Hate It
    nyoj123 士兵杀敌(四)
    poj3468 A Simple Problem with Integers
    zoj1610 Count the Colors
    nyoj144 小珂的苦恼
    nyoj93 汉诺塔(三)
    poj2182 Lost Cows
    ASP.NET2.0中的Callback机制
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/10371285.html
Copyright © 2020-2023  润新知