Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Analysis of the example of realizing the first request by Django

Shulou Source: shulou.com Published: 2022-06-02 05:59:36 09月11日 Update

This article shows you an example analysis of the Django implementation of the first request, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Request flow of Django

Write urls.py add request routing from import viewsurlpatterns = [path ('admin/', admin.site.urls), path (' test/', views.test_path), # add the request path and the function handled by the request] write the views.py under the application directory

Increase the processing logic of the corresponding request

From django.shortcuts import render,HttpResponse # Import HttpResponse module def test_path (resquest): # function name should be consistent with the print (dir (resquest)) return HttpResponse in urls.py ("first Django request") # return properties related to the content to the page view request

Request.method-- > returns the requested method (in all uppercase): GET/POST.

Request.GET-- > getting is a parameter in URL, similar to the data structure of a dictionary

Request.POST-- > data submitted by post, similar to the data structure of a dictionary

There are three ways to return a page: `from django.shortcuts import HttpResponse, render, redirect`

HttpResponse returns a specified string

Render returns a HTML file, return render (request, 'index.html')

Redirect jumps to the corresponding URL,return redirect ('/ index/')

Send data to the page

Return render (request, 'login.html', {' error_msg': error_msg}), you can use {{variable name}} to get data on the page

test

After starting the project, the test results in the browser are as follows:

The above is a sample analysis of the first request implemented by Django. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Data pages first time content examples analysis functions dictionaries skills data structures knowledge structure processing testing concise consistent concise parameters variables names Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Linux Docker Redmi