Django Rest Framework URL调度程序 [英] Django Rest Framework url dispatcher
问题描述
我正在尝试与SWAPI API进行交互.
我想要一个视图,该视图可提取所有电影( films/
),而一个视图可提取一部电影( film/id
).
当我点击 http://127.0.0.1:8000/api/films/?id=4
时,它将进入 get_films
函数.
我正在使用Django == 1.11和Django Rest Framework == 3.9.0.
我的urs.py:
urlpatterns = [url(r'films/',views.get_films,name ="get-films"),url(r'films/(?P&id; id [0-9])/',views.get_film,name ="get-film"),]
我的views.py:
MAX_RETRIES = 5API_URL =" https://swapi.co/api/@api_view(['GET','POST'])def get_films(request):request_url = API_URL +电影"打印(request_url)如果request.method =="GET":try_num = 0#跟踪我们重试了多少次而try_num<MAX_RETRIES:r = request.get(request_url,timeout = 10)如果r.status_code == 200:数据= r.json()返回响应(数据,状态=状态.HTTP_200_OK)别的:try_num + = 1#您可能可以使用记录器在此处记录错误time.sleep(5)#等待5秒钟,然后重试返回响应({错误":请求失败"},状态= r.status_code)别的:返回响应({错误":不允许使用方法"},状态=状态.HTTP_400_BAD_REQUEST)@api_view(['GET','POST'])def get_film(自己,要求):打印(输入)request_url = API_URL +电影/" + request.query_params.get('id')如果request.method =="GET":try_num = 0#跟踪我们重试了多少次而try_num<MAX_RETRIES:r = request.get(request_url,timeout = 10)如果r.status_code == 200:数据= r.json()返回响应(数据,状态=状态.HTTP_200_OK)别的:try_num + = 1#您可能可以使用记录器在此处记录错误time.sleep(5)#等待5秒钟,然后重试返回响应({错误":请求失败"},状态= r.status_code)别的:返回响应({错误":不允许使用方法"},状态=状态.HTTP_400_BAD_REQUEST)
您的实现存在两点错误.首先, get_film
.该方法中的参数是错误的.您需要这样定义它:
@api_view(['GET','POST'])def get_film(request,id):#<-第一个参数是用于`HttpRequest`对象的request.第二个是`id`,用于将其与url的id映射.打印(输入)request_url = API_URL +电影/" + str(id)#其余代码
第二,在调用此 api 时,您需要这样做: http://127.0.0.1:8000/api/films/4/
>
I am trying to interact with the SWAPI API.
I want a view that fetches all the movies(films/
) and one that fetches one (film/id
).
When I hit http://127.0.0.1:8000/api/films/?id=4
it enters the get_films
function.
I am using Django==1.11 and Django Rest Framework==3.9.0 .
my urs.py:
urlpatterns = [
url(r'films/',views.get_films,name="get-films"),
url(r'films/(?P<id>[0-9])/',views.get_film,name="get-film"),
]
my views.py:
MAX_RETRIES = 5
API_URL= "https://swapi.co/api/"
@api_view(['GET', 'POST'])
def get_films(request):
request_url = API_URL + "films"
print(request_url)
if request.method == "GET":
attempt_num = 0 # keep track of how many times we've retried
while attempt_num < MAX_RETRIES:
r = requests.get(request_url, timeout=10)
if r.status_code == 200:
data = r.json()
return Response(data, status=status.HTTP_200_OK)
else:
attempt_num += 1
# You can probably use a logger to log the error here
time.sleep(5) # Wait for 5 seconds before re-trying
return Response({"error": "Request failed"}, status=r.status_code)
else:
return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'POST'])
def get_film(self, request):
print('entered')
request_url = API_URL + "films/" + request.query_params.get('id')
if request.method == "GET":
attempt_num = 0 # keep track of how many times we've retried
while attempt_num < MAX_RETRIES:
r = requests.get(request_url, timeout=10)
if r.status_code == 200:
data = r.json()
return Response(data, status=status.HTTP_200_OK)
else:
attempt_num += 1
# You can probably use a logger to log the error here
time.sleep(5) # Wait for 5 seconds before re-trying
return Response({"error": "Request failed"}, status=r.status_code)
else:
return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
There are 2 things wrong with your implementation. First, get_film
. the arguments are wrong in this method. you need to define it like this:
@api_view(['GET', 'POST'])
def get_film(request, id): # <-- First argument is request which is for `HttpRequest` object. Second is `id` which is for mapping it with the url's id.
print('entered')
request_url = API_URL + "films/" + str(id)
# rest of the code
Second, when calling this api, you need to do it like this: http://127.0.0.1:8000/api/films/4/
这篇关于Django Rest Framework URL调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!