search
HomeWeb Front-endFront-end Q&ADjango uses request to get the parameters sent by the browser

Getting data (four ways)

1. url: 需要正则去匹配
    url(r'^index/(num)/$', view.index)
    匹配到的参数会自动传入对应的视图函数

    也可以给匹配到的参数起名字?P<num>
    url(r&#39;^index/(?P<num1>\d*)(?P<num2>\w*)$&#39;,)

    使用url传参的时候,要么都使用位置参数,要么都使用给参数起名字的方式

    也可以通过request.path获取到url然后获取相应的参数.
    如访问127.0.0.1:8000/index/
    则request.path = &#39;/index/&#39;2. ?号后面的键值对(又叫查询字符串): 
    如 index/?num=55&num2=66&num=77
    request.GET.get(&#39;num&#39;) # 获取的是77
    request.GET.getlist(&#39;num&#39;) # 获取num的所有值
    request.GET.get(&#39;num2&#39;) 

3. 请求体
    request.POST.get() 获取表单数据

    request.body 获取非表单数据,如json
    request.body返回的是一个byte的对象
        b&#39;{"key":"value"}&#39;,可以通过下面方式获取值
    data = eval(request.body.decode()).get(&#39;key&#39;)
    data1 = json.loads(request.body).get(&#39;key&#39;)    #如果request.body没有数据上面两条语句都会报错4. 报文头
    request.META 获取请求头信息,
    django会自动把获取到的请求头全部转化为大写,并在前面加上HTTP,如:
    请求头: User-Agent:***** 
    获取方式: request.META.get(&#39;HTTP_USER_AGENT&#39;)
  • Code sample (getting request data)

# 获取正则匹配到的数据,num1未位置参数,num2为命名参数,def index(request,num2=None, num1=None):
    print(num2)    # 构造响应数据
    reNT&#39;)
    sp = HttpResponse(&#39;hello world&#39;)

    # 获取请求头
    header = request.META.get(&#39;HTTP_USER_AGE)
    # 获取地址
    path = request.path    # 获取 ? 后面的数据(获取查询字符串数据)
    para = request.GET

    # 获取json数据
    json_data = request.body
    data = eval(request.body.decode()).get(&#39;asd&#39;)
    data1 = json.loads(request.body).get(&#39;asd&#39;)
    # 获取表单数据
    form_data = request.POST    # 获取请求方法
    method = request.method    # 获取文件
    file_obj = request.FILES.get(&#39;image&#39;)

    return resp

This This article explains in detail how Django uses request to obtain the parameters sent by the browser. For more information, please pay attention to the PHP Chinese website.

Related recommendations:

The problem of passing values ​​​​from the parent component to the child component echarts in vue

Javascript strict mode detailed explanation

Related code analysis of php to implement login function

The above is the detailed content of Django uses request to get the parameters sent by the browser. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the limitations of React?What are the limitations of React?May 02, 2025 am 12:26 AM

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

React's Learning Curve: Challenges for New DevelopersReact's Learning Curve: Challenges for New DevelopersMay 02, 2025 am 12:24 AM

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

Generating Stable and Unique Keys for Dynamic Lists in ReactGenerating Stable and Unique Keys for Dynamic Lists in ReactMay 02, 2025 am 12:22 AM

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScript Fatigue: Staying Current with React and Its ToolsJavaScript Fatigue: Staying Current with React and Its ToolsMay 02, 2025 am 12:19 AM

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

Testing Components That Use the useState() HookTesting Components That Use the useState() HookMay 02, 2025 am 12:13 AM

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

Keys in React: A Deep Dive into Performance Optimization TechniquesKeys in React: A Deep Dive into Performance Optimization TechniquesMay 01, 2025 am 12:25 AM

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

What are keys in React?What are keys in React?May 01, 2025 am 12:25 AM

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

The Importance of Unique Keys in React: Avoiding Common PitfallsThe Importance of Unique Keys in React: Avoiding Common PitfallsMay 01, 2025 am 12:19 AM

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use