简体   繁体   English

在python请求模块中发送用户数据

[英]Send user data in python requests module

I am unable to send the user details along with requests module i had to hard code the user details in the data payload to identify the user. 我无法将用户详细信息与请求模块一起发送,我不得不在数据有效负载中对​​用户详细信息进行硬编码以识别用户。

    full_url = ''.join(['http://', get_current_site(request).domain, '/am/reply'])
    data = {    
               'agent_type':'trigger',
               'input':platform,
               'userid':request.user.id ####==>> had to send userid like this
           } 
    a = requests.get(full_url,params=data)

Is there way to send all general request data using requests.? 是否可以使用请求发送所有常规请求数据?

And moreover the requests url the destination view i have implemented 而且请求将URL指向我已实现的目标视图

def index(request):
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse('login'))

And request.user.id is none when url is reached through requests module In general how should i validate a request when using requests module 当通过请求模块到达URL时,request.user.id为none。通常,使用请求模块时,我应如何验证请求

Django uses request and response objects to pass state through the system. Django使用请求和响应对象在系统中传递状态。

When a page is requested, Django creates an HttpRequest object that contains metadata about the request. 当请求页面时,Django创建一个HttpRequest对象,该对象包含有关请求的元数据。 Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function. 然后Django加载适当的视图,并将HttpRequest作为第一个参数传递给视图函数。 Each view is responsible for returning an HttpResponse object. 每个视图负责返回HttpResponse对象。

Some of the middleware included in Django's contrib apps set attributes on the request. Django contrib应用程序中包含的某些中间件可根据请求设置属性。 If you don't see the attribute on a request, be sure the appropriate middleware class like authenticationmiddleware,sessionmiddleware. 如果您在请求中看不到该属性,请确保使用适当的中间件类,例如authenticationmiddleware,sessionmiddleware。

Following piece of code will give the user.id if and only if the user is authenticated. 以下代码段将在且仅当用户通过身份验证时才提供user.id

def myview(request):
    if request.user.is_authenticated:
        print request.user.id
    else:
        ... # Do something else.

https://docs.djangoproject.com/en/1.10/ref/request-response/ https://docs.djangoproject.com/en/1.10/ref/request-response/

If I understood your question correctly, You are getting request in one view, and then making a call to other view using requests module. 如果我正确理解了您的问题,那么您将在一个视图中获取请求,然后使用请求模块调用另一个视图。 It that case the request object in index view will be totally different because that request was sent from your server where application works, not from user. 这种情况下,索引视图中的请求对象将完全不同,因为该请求是从应用程序运行所在的服务器而不是用户发送的。 You can only get data in index view using request.GET.get("userid") and so on. 您只能使用request.GET.get("userid")等在索引视图中获取数据。 And then if you will need user info, just fetch it again from database using userid. 然后,如果您需要用户信息,只需使用userid从数据库中再次获取它即可。 Passing request object to other view using requests library is not possible. 无法使用请求库将请求对象传递到其他视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM