在 GET 请求期间在 Django 中进行身份验证 [英] Authentication in Django during GET request

查看:45
本文介绍了在 GET 请求期间在 Django 中进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过一个 android 应用程序向我的 Django 服务器发出一个 GET 请求.问题是这个 GET 函数是用 Django 编写的,带有 @login_required.

我应该做什么样的请求来登录和执行 GET 请求?

我的 API:

from django.shortcuts 导入渲染从 django.http 导入 HttpResponse,HttpResponseServerError, HttpResponseBadRequest从 rest_framework.decorators 导入 api_view从 .models 导入订单,新建导入json从 django.shortcuts 导入 render_to_response从 .forms 导入 NewForm从 django.core.files.storage 导入 FileSystemStorage从 django.contrib.auth.decorators 导入 login_required从 .validation 导入 *从 django.views.decorators.csrf 导入 csrf_exempt@要求登录@api_view(['GET'])def get_order(请求):order_list = Order.objects.values("user_name","user_surname","order_date").all()返回 HttpResponse(json.dumps([x for x in order_list])

我的 urls.py:

from django.urls 导入路径从 django.conf.urls 导入 url从 .导入视图app_name = 'API'网址模式 = [url(r'^$', views.index, name='index'),url(r'^order', views.order, name='order'),url(r'^get_order', views.get_order, name='get_order')]

和:

from django.contrib import admin从 .导入设置从 django.views.static 导入服务from django.urls 导入路径,包含,re_path从 django.conf.urls.static 导入静态从 API.views 导入索引网址模式 = [path('API/', include('API.urls',namespace="API")),路径('管理员/',admin.site.urls),路径('',索引,名称='索引'),路径(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),re_path(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT})]

在 Postman 中,我正在执行 GET (

UPDATE-2
改变你的看法如下

 from rest_framework.decorators import api_view,permission_classes从 rest_framework.permissions 导入 IsAuthenticated从 rest_framework.response 导入响应@permission_classes((IsAuthenticated,))@api_view(['GET'])def get_order(请求):order_list = Order.objects.values("user_name","user_surname","order_date").all()返回响应(数据=[x for x in order_list])

I need to do a GET request to my Django server through an android application. The problem is that this GET function is written in Django with a @login_required.

What kind of request should i do to login and perform the GET request?

My API:

from django.shortcuts import render
from django.http import HttpResponse,HttpResponseServerError, HttpResponseBadRequest
from rest_framework.decorators import api_view
from .models import Order, New
import json
from django.shortcuts import render_to_response
from .forms import NewForm
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.decorators import login_required
from .validation import *
from django.views.decorators.csrf import csrf_exempt
@login_required
@api_view(['GET'])
def get_order(request):

    order_list = Order.objects.values("user_name",
                        "user_surname",
                        "order_date").all()

    return HttpResponse(json.dumps([x for x in order_list])

My urls.py:

from django.urls import path
from django.conf.urls import url
from . import views

app_name = 'API'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^order', views.order, name='order'),
    url(r'^get_order', views.get_order, name='get_order')]

and:

from django.contrib import admin
from . import settings
from django.views.static import serve
from django.urls import path, include, re_path
from django.conf.urls.static import static
from API.views import index

urlpatterns = [
    path('API/', include('API.urls',namespace="API")),
    path('admin/', admin.site.urls),
    path('', index, name='index'),
    path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})
]

In Postman i'm performing a GET (http://127.0.0.1:8000/API/get_order) with basic auth, receiving 302 status code.

解决方案

I'm not sure how this can be done in Android side, but the basic idea is that you should add authentication credentials in your GET request.

I'll show you an example of the same by using Python's Requests library

from requests.auth import HTTPBasicAuth
import requests

requests.get('http://my/url/end/poin/', auth=HTTPBasicAuth('my_username', 'mypass'))



May be these results will help you to find the answer ;)


UPDATE

1. go to Authorization tab
2. choose Basic Auth type
3. Provide your username and password in respective text boxes
4. hit the send button

UPDATE-2
change your view as below

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response


@permission_classes((IsAuthenticated,))
@api_view(['GET'])
def get_order(request):
    order_list = Order.objects.values("user_name",
                                      "user_surname",
                                      "order_date").all()

    return Response(data=[x for x in order_list])

这篇关于在 GET 请求期间在 Django 中进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆