Django -- httprequest object, httpresponse object

Jocelyn bear 2022-01-26 15:45:33 阅读数:194

django httprequest object httpresponse object

about HttpRequest Object knowledge , On the basis of the completion of the previous article .

1.HttpRequest object

(1)GET attribute

    establish test The view function ;

def test(request):
if request.method == 'GET':
print("method:",request.method)
print("get:",request.GET)
return render(request,'book/test.html')
else:
print("method:",request.method)
print("post:",request.POST)
print("path:",request.path)
return HttpResponse("ok")

    establish test.html And configure url;

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="/book/test">
<input type="text" name="name">
<input type="text" name="age">
<button> Submit </button>
</form>
</body>
</html>
 url(r"^test",test),

    The effect is as follows : 

     

     

(2)POST attribute

    Take adding books as an example , First create a create.tml Template file ;

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/book/create" method="post">
Title :<input type="text" name="bname"><br>
Page number :<input type="text" name="bpage"><br>
Publication date :<input type="text" name="bpub_date">
<button> Submit </button>
</form>
</body>
</html>

    Definition create View functions and configure url; 

def create(request):
if request.method == 'GET':
# Render the form page of the added book
return render(request,'book/create.html')
else:
bookinfo = BookInfo()
bookinfo.bname = request.POST.get('bname')
bookinfo.bpage = request.POST.get("bpage")
bookinfo.bpub_date = request.POST.get('bpub_date')
bookinfo.save()
return HttpResponseRedirect("/book/index")
 url(r"^create",create),

      The effect is as follows :

    

  Be careful : It needs to be commented out here settings.py In the document MIDDLEWARE Of crsf Content ;

# 'django.middleware.csrf.CsrfViewMiddleware',

 2.HttpResponse object

    Take deleting books as an example , First create a delete View ;

from django.http import HttpResponseRedirect
def delete(request,bid):
# 1. adopt bid Search for Book
book = BookInfo.objects.get(id=bid)
# 2. Delete
book.delete()
# 3. Redirect , Let the browser access /index
return HttpResponseRedirect("/book/index")

    Direct configuration routing ;

 url(r"^delete/(\d+)",delete),

    In the original creation index.html Add the following code to the template :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Book information </title>
</head>
<body>
<ul>
<a href="/book/create"> add to </a>
{% for book in books%}
<li>{
{book.bname}},{
{book.bpage}}-- --<a href="/book/edit?id={
{book.id}}"> modify </a> <a href="/book/delete/{
{book.id}}"> Delete </a></li>
{% endfor %}
</ul>
</body>
</html>

    The effect is as follows :( After deleting )

 

copyright:author[Jocelyn bear],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261545313208.html