Chapter VI Django form

A, method and object properties HttpRequest

request.path: full path does not contain the domain name, comprising a leading slash, such as: / helllo /

request.get_host (): domain name, such as: http: //127.0.0.1: 8080

request.get_full_path (): a path containing the query string, such as: / hello / id = 1?

request.is_secure (): access to True by HTTPS, otherwise False

request.GET: get get data through forms and URL

request: POST; post data acquisition, through the form

Second, the form processing example

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)

 

Guess you like

Origin www.cnblogs.com/wenwu5832/p/11909241.html