• Python Django 的使用


    Django
    1. 创建project
      diango-admin startproject mysiste

      # project
      mysite
        mysiste # 配置文件
          - settings.py # 配置文件
          - urls.py # 路由系统,一个URL指向一个函数
          - wsgi.py # WSGI

      manage.py # django程序启动文件

    2.创建APP 一个project有多个APP
      1.cd mysite
      2.python manage.py startapp APP名称

    3.简单的编写代码
      urls.py 添加映射关系
      view.py 添加函数

    4.启动django程序
      1.python manage.py runserver 127.0.0.1:8000
      2.pycharm启动
        设置好IP和端口
        启动django程序

    5.如果要使用模板
      在templates加html,当然 也可以在settings.py中配置html的目录
      在views里的函数中,直接return render(requset,'01.html')


    6.使用静态模板
      如果要使用jquery,要在配置文件中设置
      STATIC_URL = '/static/'
      STATICFILES_DIRS=(
      os.path.join(BASE_DIR,'statics'),
      )


      其中上面STATIC_URL = '/static/'的static 与
      使用时<script src="/static/jquery-3.5.0.js"></script> 中的static要一致,也可以随意更改名字

    7.表单提交

      1.可以提交数据给HTML return render(request, 'index.html',{'data':USER_INPUT})
      2.特殊语句,如for循环需要 {% for item in data %} 和结束{% endfor %} 中间的数据是{{ item.user }}

    问题:
    1.出现:Forbidden (CSRF cookie not set.): /index/
    到settings.py 把MIDDLEWARE中的'django.middleware.csrf.CsrfViewMiddleware', 注释掉
    2.提交时后台没反应
    if(request.method=='POST'): 中的POST要大写 不是小写

    不连接数据库的例子:

     1 from django.shortcuts import render
     2 from django.shortcuts import HttpResponse
     3 # Create your views here.
     4 USER_INPUT=[
     5 {'user':'u1','email':'e1'},
     6 {'user':'u2','email':'e2'},
     7 ]
     8 def index(request):
     9     # return HttpResponse('123')
    10     if(request.method=='POST'):
    11         user=request.POST.get('user',None)        # 如果想要获取不存在的东西就会报错
    12         email=request.POST.get('email',None)       #  所以需要应该默认值,第二个参数就是默认值
    13         temp={'user':user,'email':email}
    14         USER_INPUT.append(temp)
    15     # 模板引擎,获取index.html模板 然后把数据{'data':USER_INPUT} 渲染
    16     # 如:把{% for item in data %} {% endfor %} 变成Python中的for循环
    17     return render(request, 'index.html',{'data':USER_INPUT})
    Views
    1 from django.contrib import admin
    2 from django.urls import path
    3 from cmdb import views
    4 urlpatterns = [
    5     path('admin/', admin.site.urls),
    6     path('index/',views.index)
    7 ]
    url.py
    1 STATIC_URL = '/static/'
    2 STATICFILES_DIRS=(
    3     os.path.join(BASE_DIR,'statics'),
    4 )
    setings.py
     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8     <h1>用户输入:</h1>
     9     <form action="/index/" method="post">
    10         <input type="text" name="user">
    11         <input type="text" name="email">
    12         <input type="submit" name="提交">
    13     </form>
    14     <h1>数据展示:</h1>
    15     <table border="1">
    16         {% for line in data %}
    17             <tr>
    18                 <td>{{ line.user }}</td>
    19                 <td>{{ line.email }}</td>
    20             </tr>
    21         {% endfor %}
    22     </table>
    23     <script src="/static/jquery-3.5.0.js"></script>
    24 </body>
    25 </html>
    templas


    8.连接数据库,操作数据库
    ORM

      modals.py
        class UserInfo(models.Model): # 需要继承类
          user = models.CharField(max_length=32)
          email = models.CharField(max_length=32)

      注册APP:
        在setings.py 中的 INSTALLED_APPS 添加APP名称

      创建数据库:
        执行命令:
        python manage.py makemigrations
        python manage.py migrate

    9.操作数据库:
      创建:models.类.objects.create()
      获取:models.类.objects.all()

    连接数据库则需要替换

     1 from django.shortcuts import render
     2 from django.shortcuts import HttpResponse
     3 from cmdb2 import models
     4 
     5 # Create your views here.
     6 
     7 def index(request):
     8     # return HttpResponse('123')
     9     if(request.method=='POST'):
    10         u=request.POST.get('user',None)        # 如果想要获取不存在的东西就会报错
    11         e=request.POST.get('email',None)       #  所以需要应该默认值,第二个参数就是默认值
    12         models.UserInfo.objects.create(user=u,email=e)
    13     # 模板引擎,获取index.html模板 然后把数据{'data':USER_INPUT} 渲染
    14     # 如:把{% for item in data %} {% endfor %} 变成Python中的for循环
    15     data_list = models.UserInfo.objects.all()
    16     return render(request, 'index.html',{'data':data_list})
    Views
    1 from django.db import models
    2 
    3 # Create your models here.
    4 class UserInfo(models.Model):  # 需要继承类
    5     user = models.CharField(max_length=32)
    6     email = models.CharField(max_length=32)
    models.py
    1 from django.contrib import admin
    2 from django.urls import path
    3 from cmdb2 import views
    4 urlpatterns = [
    5     path('admin/', admin.site.urls),
    6     path('index/',views.index)
    7 ]
    url.py

    setings.py里添加

    1 INSTALLED_APPS = [
    2     'django.contrib.admin',
    3     'django.contrib.auth',
    4     'django.contrib.contenttypes',
    5     'django.contrib.sessions',
    6     'django.contrib.messages',
    7     'django.contrib.staticfiles',
    8     'cmdb2',
    9 ]
    setings.py
  • 相关阅读:
    页面跳转时,统计数据丢失问题探讨
    JSBridge 知识点
    数据埋点 知识点
    ES6 模块与 CommonJS 模块的差异
    koa 学习资料
    浏览器渲染流程
    Object.create() 的含义:从一个实例对象,生成另一个实例对象
    this、new,容易混淆的地方
    为什么js 的constructor中是无限循环嵌套:Foo.__proto__.constructor.prototype.constructor.prototype.constructor.prototype.xxx ?
    实例对象与 new 命令
  • 原文地址:https://www.cnblogs.com/otome/p/12823749.html
Copyright © 2020-2023  润新知