程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Problems and solutions of Django cooperating with Python to make requests

編輯:Python

Catalog

Django coordination python Conduct requests request

Preface

Django Code section

rest_framework

api/views.py

INSTALLED_APPS

Local interface

Django coordination python Conduct requests request Preface

When we write code , We often use the method of separating the front end from the back end , For example, wechat applet , Android , Website, etc. …
that Python As a popular programming language , His built-in Django Framework is a good network framework , Can be used to build the back end , Interact with the front end . Now let's learn , How to use Python It's local requests request , And make... By request Django Help us solve some problems .

Django Code section

First, create a Django After the project , You will find that the official has already configured a lot of files for us . But these documents are still not enough , We need to create another one app. Then it can be directly at the terminal (terminal) Enter a line of instructions in .

python manage.py startapp api

After entering this line of instructions , We will find that , In our code, there is a called api Folder .

rest_framework

Now let's open up api Under folder views.py file . Then you can see that there is nothing in it .

Now let's write a simple interface . This interface is used to receive requests request , And returned after processing . His essence is a class. Let's write a simple demo, The code is as follows :

api/views.pyfrom rest_framework.views import APIViewfrom rest_framework.response import Responseclass demo(APIView): def __init__(self, **kwargs): super().__init__(**kwargs) def post(self, request, *args, **kwargs): print(request.data) return Response({'message': True})

Let's take a look at the project file urls.py The meaning of this file is to configure the address of the web page and the interface content of the page .

from django.contrib import adminfrom django.urls import pathfrom api import viewsurlpatterns = [ path('admin/', admin.site.urls), path('login/', views.demo.as_view())]

In this way , After we run django After the project , We can add... Directly after the port of the web address /login/, You can see us directly demo The interface of .

INSTALLED_APPS

After writing the above section , We need to write one more thing , Otherwise our framework May not work properly . So let's find the... In the code setting.py. find INSTALLED_APPS.

After finding it, let's add the same parameter "rest_framework".

Local interface

Now we're running Django Interface , But we didn't write the test part , So we're not sure if our code is correct .
The code is as follows :

import requestsurl = 'http://127.0.0.1:8000/login/'r = requests.post(url=url, data={'test': "heiheihei", 'mes':"666"})ui = r.textprint(ui)

After running, we can see that a parameter is received locally {‘message’: True}. So our django and Python The local interaction is finished .

This is about Django coordination python Conduct requests That's all for the requested article , More about Django python requests Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you can support SDN in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved