首页 > 其他 > 详细

请求与响应

时间:2019-03-11 00:17:37      阅读:174      评论:0      收藏:0      [点我收藏+]

 

一、HttpRequest的常用属性和方法

技术分享图片

技术分享图片

技术分享图片

二、前端form表单

在HTML中,form表单的作用是收集标签中的内容,<form>...</form> 中间可以由访问者添加类似于文本,选择,或者一些控制模块等等.然后这些内容将会被送到服务端。

一个表单必须指定两样东西:

1. form的method参数用于设置表单的提交方式,默认使用POST.

2. action用于设置表单的提交url,如果不写或者保持空字符串,那么将使用当前的URL.

 form表单,login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <h1>登录</h1>
    <form action="" method="post">  #action指的是提交地址,不写或者为空代表提交单当前页。method请求方法,默认不写为post请求。收集表单数据,返回给页面
        {% csrf_token %}    #解决403 Forbidden。每一次form表单都要加这个令牌,标签
        <p>用户名:<input type="text" name="username"></p>
        <p>密码:<input type="password" name="password"></p>
        <p><input type="submit" value="登录"></p>
    </form>
</body>
</html>

添加teacher.views

from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse,JsonResponse
from datetime import datetime
from django.template.loader import get_template
from teacher.models import Student,Grade,StudentDetail,Course,Enroll

from crm.settings import UPLOAD_ROOT
from django.db.models import Q
import os

# Create your views here.
def index(request):
    students = Student.objects.all()
    arg = {
        _meta:aaaa
    }
    format_str = %Y-%m-%d %H:%M:%S
    return  render(request,teacher/index.html,context={
        student:students,
        format_str:format_str
    })

def login(request):
    if request.method == POST:
        username = request.POST.get(username,‘‘)
        password = request.POST.get(password,‘‘)
        if username == xinlian and password == 111:
            return  redirect(reverse(teacher:index))
    return  render(request,teacher/login.html)

添加teacher.urls.py

urlpatterns = [
    path(index/, views.index,name=index),
    path(login/, views.login),
]

三、前后端交互登录小案例(代码如上面所示)

技术分享图片


4.文件上传

如果URL的get方法中传递两个相同的参数

http://127.0.0.1:8000/teacher/login/?hobby=111&hobby=222

需要使用getlist方法才能获取到完整数据

技术分享图片

 

请求与响应

原文:https://www.cnblogs.com/taoge188/p/10508133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!