一、HttpRequest对象的方法和属性
request.path:完整路径,不包含域名,包含前导斜线,如:/helllo/
request.get_host():域名,如:http://127.0.0.1:8080
request.get_full_path():包含查询字符串的路径,如:/hello/?id=1
request.is_secure():通过HTTPS访问为True,否则为False
request.GET:获取get数据,通过表单和URL
request:POST;获取post数据,通过表单
二、表单处理实例
urls.py
from django.urls import path,re_path from books.views import search_form,search urlpatterns = [ path('search_form',search_form), re_path(r'^search/$',search), ]
search_form.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>search</title> </head> <body> <form action="/books/search/" method="get"> <input type="text" name="q"/> <input type="submit" value="search"/> </form> </body> </html>
views.py
from django.shortcuts import render from django.http import HttpResponse # Create your views here. def search_form(request): return render(request,'search_form.html') def search(request): if 'q' in request.GET: message = 'your search for %r' % request.GET['q'] else: message = 'you submitted empyt' return HttpResponse(message)