Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从前端使用django身份验证登录_Python_Django - Fatal编程技术网

Python 从前端使用django身份验证登录

Python 从前端使用django身份验证登录,python,django,Python,Django,我已经创建了一个允许用户登录的视图 def signin(request): email = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=email, password=password) if user is not None: login(request, user) 如何验证用户是否已登

我已经创建了一个允许用户登录的视图

def signin(request):

    email = request.POST['username']
    password = request.POST['password']
    user = authenticate(request, username=email, password=password)
    if user is not None:
        login(request, user)
如何验证用户是否已登录

是在用户访问API时要求用户首先登录吗


文档显示,我们可以使用
request.user.is\u authenticated
is
True
进行验证

但是,我并没有使用django模板,而是使用
reactJS
发送POST请求

我该怎么做


我是否应该在POST请求的json正文中包含一个
User
属性?

无论您是使用django模板还是使用reactjs发送POST请求。因此,该函数将位于views.py文件中,您可以为所有必需的端点添加@login_required decorator,django拥有自己的身份验证(默认情况下,它将与用户模型关联,但您可以更改它)以保护API,并且您可以在应用程序中使用DRF令牌或JWT来发布服务(有关详细信息,请检查此项:)

request.user.is\u authenticated
在Python代码中工作得非常好,它不仅仅适用于Django模板。例如,我有一个名为/order\u food/的API,它要求用户登录。order_food POST请求通过Axios发送。我应该在POST请求中提供什么样的信息,让django知道是用户发送的?如果您使用的是API,那么您必须使用JWT来实现这一点,如果这是您的意思的话