除了ForeignKey,ManyToManyFiled,OneToOne,所在字段可以可以同类名相同,其他字段都不允许和类名相同
请求与响应
一,HttpRequest对象
django框架 --> Web应用框架
请求和响应是框架中的数据流
request的属性
二,GET和POST请求
1),GET请求
/*form表单模拟*/
<form action="" method="get"> {# action如果不写代表跳转到当前页 #}
<p>用户名: <input type="text" name="username"></p>
<p>密 码: <input type="password" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
通过get请求传输过来的所有数据可以通过getlist
这个方法拿到
2),POST方式
/*form表单模拟*/
<form action="" method="post"> {# action如果不写代表跳转到当前页 #}
{% csrf_token %} /*服务器允许传递数据*/
<p>用户名: <input type="text" name="username"></p>
<p>密 码: <input type="password" name="password"></p>
<p><input type="submit" value="登录"></p>
</form>
3,),前后台交互流程
'''
输入url,get请求,服务器响应,返回登录页面。输入用户名,密码提交按钮,发送post请求,提交数据,通过验证,跳转到主页
'''
总结:
1.GET:是从服务器获取数据,携带参数会直接显示在url中,不更改服务器数据
2.POST:向服务器发送数据,一般会更改服务器数据,参数不会显示在url中
3.POST请求需要带{% csrf_token %},Django有安全防御机制,需要这个才能访问服务器
三,文件上传
-
1. 要设置文件的存储路径 2. 在项目根目录下的static文件夹中创建media文件夹 3. 在setting中添加MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')
-
# 第二种方法(了解就行) a.在项目根目录下创建一个新的文件夹(upload) b.在setting文件中添加UPLOAD_ROOT = os.path.join(BASE_DIR, 'upload')
# 模拟文件上传 <form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file" multiple="multiple"> <input type="submit" value="上传"> </form> ... def upload(request): if request.method == 'POST': # file = request.FILES.get('file') # 上传一个文件 files = request.FILES.getlist('file') # 上传多个文件 # 每天上传的文件放入到每天的文件夹,创建以日期为名的文件夹 day_dir_name = datetime.now().strftime('%Y%m%d') day_dir = os.path.join(MEDIA_ROOT, day_dir_name) if not os.path.exists(day_dir): os.mkdir(day_dir) # 保存单个文件 # file_url = os.path.join(day_dir, file.name) # with open(file_url, 'wb') as fb: # for line in file.chunks(): # 文件上传过大时,自动分块 # fb.write(line) # 保存多个文件 for file in files: file_url = os.path.join(day_dir, file.name) with open(file_url, 'wb') as fb: for line in file.chunks(): # 文件上传过大时,自动分块 fb.write(line) return render(request, 'student/upload.html')
四,HttpResponse
HttpRequest对象是django自动创造的,HttpResponse对象使我们手动创建的
from django.http import HttpResponse
In [2]: response = HttpResponse('上传成功')
In [3]: response.content
Out[3]: b'\xe4\xb8\x8a\xe4\xbc\xa0\xe6\x88\x90\xe5\x8a\x9f'
In [4]: response.content.decode('utf-8')
Out[4]: '上传成功'
In [5]: response
Out[5]: <HttpResponse status_code=200, "text/html; charset=utf-8">
In [6]: response = HttpResponse()
In [7]: response.write('hello')
In [8]: response.write('world')
In [9]: response.write('<h2>world</h2>')
In [10]: response.content.decode('utf-8')
Out[10]: 'helloworld<h2>world</h2>'
# 使用Json格式
from django.http import HttpResponse, JsonResponse
def text(request):
sex = int(request.GET.get('sex'))
res = Imformation.objects.values('name', 'age', 'sex').filter(sex=sex)
res = list(res)
data = {'result': res}
return JsonResponse(data)
五,cookies
def index(request):
num = request.COOKIES.get('num')
if num:
num = int(num) + 1
else:
num = 1
response = render(request, 'student/index.html', context={'num': num})
response.set_cookie('num', num, max_age=5) # 值为整数,单位为秒,设置cookie的有效时间为5秒;
return response