• django前后端交互


    前后端交互的方式有两种,一种是自己写个html页面,插入数据,一种是使用django自带的后台管理,插入数据

    下面介绍方式1:

    post.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form action="/post" method="post">
    
        {% csrf_token %}
    
        <h1>写文章的</h1>
        <div>
                title:<input type="text" name="title">
    
        </div>
        <div>
                content:<input type="text" name="content">
    
        </div>
    
        <div>
            <select name="category">
                {% for category in categories %}
                <option value="{{ category.id }}">{{ category.name }}</option>
                {% endfor %}
            </select>
        </div>
    
        <div>
                <input type="submit" value="提交">
    
        </div>
    
    </form>
    
    
    </body>
    

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

    form这里指定是post还是get过来的数据走这个表单

    urls.py里面的前面的那个路径,要和action的一致,如下:path('post',views.article),

    {% csrf_token %}: csrf: 为了防止重复提交的,每次请求页面都会在form表单里面随机加一个csrf字符串,这个字符串是隐藏的,如果短时间内提交过快的话,csrf是一样的,会被认为是重复提交的,那么第二次请求就会被认为是不合法的,,解决方法:

    1.在settings里面注释这个验证,不让django进行验证

    MIDDLEWARE = [
        'django.middleware.security.SecurityMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        # 'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
        'user.middle_wares.TestMiddleWare'
    ]
    

     2. form表单中添加{% csrf_token %}

    views.py

    def article(request):
        print(request.method) #请求方式
        print(request.GET)# url?key=vlauye
        print(request.POST)#url  key-vlaue
        print(request.COOKIES)
        print(request.path_info)  #请求的路径 /post /cate
        print(request.FILES) #获取文件
        print(request.META) #请求头相关的都在这里
        print(request.body) #body里面的内容
    
    
        if request.method=='GET':
            return render(request,'post.html')
        else:
            title = request.POST.get('title')
            content = request.POST.get('content')
            category = request.POST.get('category')
            models.Article.objects.create(title=title,content=content,category_id=category)
            return HttpResponseRedirect('/')
    

     HttpResponseRedirect()是指重定向到哪个页面

  • 相关阅读:
    条形码:Code93
    MSSQL 又一个行列转换
    PowerShell查看Sharepoint日志
    SharePoint Meeting Workspace
    Developer Dashboard in SharePoint 2010
    SPUtility.SendEmail
    处理sharepoint 列表中的 person or group类型字段
    SharePoint Issue: Unable to oben the Site (Object null reference Error) Solution
    import a Microsoft SharePoint 2010 site definition
    SPListTemplateType 枚举 (Microsoft.SharePoint) 创建列表时的ListTemplate Type属性
  • 原文地址:https://www.cnblogs.com/liulilitoday/p/13580788.html
Copyright © 2020-2023  润新知