django request.POST包含<无法解析> [英] django request.POST contains <could not parse>

查看:129
本文介绍了django request.POST包含<无法解析>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django表单来获取用户名,密码。当用户发布数据时,我看到后字典包含以下(追溯),
追溯(最近的最后一次调用):

 文件/usr/lib/python2.4/site-packages/django/core/handlers/base.py,第111行,in get_response 
response = callback(request,* callback_args,* * callback_kwargs)

文件/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py,第39行,wrapped_view
resp = view_func( * args,** kwargs)

文件/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py,第52行在wrapped_view
return view_func(* args,** kwargs)

文件/public/gdp/trunk/src/ukl/lis/process/utils/error_handler.py,第17行,__call__
return self.function(* args,** kwargs)

文件/usr/lib/python2.4/site-packages/django/views/decorators/cache.py,第66行,在_cache_controlled
response = viewfunc(request,* args,** kw)

文件/ public / gdp / trunk / src / ukl / lis / process / authentication / view s.py,line 530,in process_login
form = loginForm(request.POST)

文件/usr/lib/python2.4/site-packages/django/core/handlers /modpython.py,第101行,在_get_post
self._load_post_and_files()

文件/usr/lib/python2.4/site-packages/django/http/__init__.py ,第270行,_load_post_and_files
如果self.META.get('CONTENT_TYPE','').startswith('multipart'):

AttributeError:'NoneType'对象没有属性'startswith'

< ModPythonRequest
路径:/login.html,
GET:< QueryDict:{}>
POST:< can not parse>
COOKIES:{'__ utma':'115966011.1553834174.1346687405.1346687405.1346687045.1',
'__utmb':'115962011.4.10.1346687045',},
META:{'AUTH_TYPE' ,
'CONTENT_LENGTH':'85',
'CONTENT_TYPE':None,
'GATEWAY_INTERFACE':'CGI / 1.1',
'HTTP_ACCEPT':'text / html, application / xhtml + xml,* / *',
'HTTP_ACCEPT_ENCODING': 'gzip,deflate',
'HTTP_ACCEPT_LANGUAGE':'en-GB',
'HTTP_CACHE_CONTROL':'no-cache',
'HTTP_CONNECTION':'Keep-Alive',
'HTTP_CONTENT_LENGTH':'85',
'HTTP_COOKIE':'flavor = desktop;的sessionid = 4a2f2ab6f61315493f3038338524cfc7; tmsid = e7c921af-9cae-4f58-8825-13f9bc2ba95f; uniqid = 6f69c607-6aca-4e92-A112-b83691805155; __utma = 115962011.1553833174.1346687005.1346687005.1346687005.1; __utmb = 115962011.4.10.1346687005; __utmz = 115962011.1346687005.1.1.utmcsr =(直接)| utmccn =(直接)| utmcmd =(无); __utmc = 115962011',
'HTTP_HOST':'example.com',
'HTTP_REFERER':'http://example.com/',
'HTTP_USER_AGENT':'Mozilla / 5.0 (兼容MSIE 9.0; Windows NT 6.1; WOW64; Trident / 5.0)',
'PATH_INFO':u'/ login.html',
'PATH_TRANSLATED':无,
'QUERY_STRING ':无,
'REMOTE_ADDR':'xx.xx.xx.xx',
'REMOTE_HOST':无,
'REMOTE_IDENT':无,
'REMOTE_USER'没有,
'REQUEST_METHOD':'POST',
'SCRIPT_NAME':'',
'SERVER_NAME':'example.com',
'SERVER_PORT':443,
'SERVER_PROTOCOL':'HTTP / 1.1',
'SERVER_SOFTWARE':'mod_python'}>

为什么post dict有<无法解析为什么CONTENT_TYPE无?这使我无法跨越登录页面。



这是我的表单,

 < form method =POST> {%csrf_token%} 
< tr>
< td>< label>电子邮件ID< / label>< / td>
< td> {{form.username}}< / td>
< td>< label>密码< / label>< / td>
< td> {{form.password}}< / td>
< / tr>
< input type =submitname =btn_loginid =btn_loginclass =btn_loginvalue =Login/>< / td>

< / form>

Forms.py:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ autocomplete:off}))

class Meta:
model =用户

查看:

  def login(request):
if request.method == POST:
log.inof(request)
else:
request.session.set_test_cookie()
form = loginForm()
返回render_to_response('login.html ',{'form':form},context_instance = RequestContext(request))

注意:发现所有请求都只来自于移动设备,尤其是Blackberry。看来还有一个用户有同样的问题



REF: https://stackoverflow.com/questions/12471661/mod-python-could-not-parse-the-django-post-request -for-blackberry-and-some-andro

解决方案

由于您的开发栈不会发生错误,只是有时候在生产中我怀疑这是Django代码中的一些。



你如何联系机器?请求将发送到localhost ..您是在生产机器上运行浏览器还是在其前面有一些代理或负载均衡器?



在我看来,在他们点击mod_python(无论是代理还是配置错误的ssl)之前,有一些东西让您的请求变得更糟。



您的症状是您收到破坏的请求。这个问题可能在你的python应用程序的所有OSI层的任何地方,最终在客户端上的所有OSI层。尝试对所有内容进行全面的了解,并系统地对其进行排除。



您的TCP / IP协议栈似乎工作正常,或者您会看到其他请求和服务遭受损失太。所以我们可以排除会话层之下的所有内容。这将留下演示层(SSL)和应用层(HTTP)。


  1. SSL



    SERVER_PORT是443,SSL在这里播放。请求标头(HTTP_REFERER等)包含有效的字符串,所以SSL似乎工作正常。但是我之前见过一些奇怪的TLS会话。


  2. HTTP



    这个层:




    • 你的Apache httpd

    • 你的结尾的任何反向代理

    • 您和客户端之间的任何转发代理。 REMOTE_ADDR都在同一个子网中,有没有一些移动运营商在那里破碎的代理?

    • 客户端用户代理。所有黑莓都破了吗?尝试让您的手在您看到失败的浏览器上。


你的负载均衡器运行在什么层面?



看起来主要的问题是在POST请求上缺少Content-Type头,因此POST内容的Python表示形式的值不能使用pprint打印,更不用说由您的应用程序使用。



忽略什么我发现在Blackberry开发人员ref中:你是否尝试将enctype设置为 Simon建议?尝试改变 application / x-www-form-urlencoded multipart / form-data



最好的尝试重现它自己。尝试制作记录此类请求的 tcpdump 。您可以使用 wireshark 分析记录的转储。


I am having a django form to get the username,password. when the user posts data, i see that the post dictionary contains the following(traceback), Traceback (most recent call last):

File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py", line 39, in wrapped_view
resp = view_func(*args, **kwargs)

File "/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py", line 52, in wrapped_view
return view_func(*args, **kwargs)

File "/public/gdp/trunk/src/ukl/lis/process/utils/error_handler.py", line 17, in __call__
return self.function(*args, **kwargs)

File "/usr/lib/python2.4/site-packages/django/views/decorators/cache.py", line 66, in _cache_controlled
response = viewfunc(request, *args, **kw)

File "/public/gdp/trunk/src/ukl/lis/process/authentication/views.py", line 530, in process_login
form = loginForm(request.POST)

File "/usr/lib/python2.4/site-packages/django/core/handlers/modpython.py", line 101, in _get_post
self._load_post_and_files()

File "/usr/lib/python2.4/site-packages/django/http/__init__.py", line 270, in _load_post_and_files
if self.META.get('CONTENT_TYPE', '').startswith('multipart'):

AttributeError: 'NoneType' object has no attribute 'startswith'

<ModPythonRequest
path:/login.html,
GET:<QueryDict: {}>,
POST:<could not parse>,
COOKIES:{'__utma': '115966011.1553834174.1346687405.1346687405.1346687045.1',
'__utmb': '115962011.4.10.1346687045',},
META:{'AUTH_TYPE': None,
'CONTENT_LENGTH': '85',
'CONTENT_TYPE': None,
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': 'text/html, application/xhtml+xml, */*',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
'HTTP_ACCEPT_LANGUAGE': 'en-GB',
'HTTP_CACHE_CONTROL': 'no-cache',
'HTTP_CONNECTION': 'Keep-Alive',
'HTTP_CONTENT_LENGTH': '85',
'HTTP_COOKIE': 'flavor=desktop; sessionid=4a2f2ab6f61315493f3038338524cfc7;    tmsid=e7c921af-9cae-4f58-8825-13f9bc2ba95f; uniqid=6f69c607-6aca-4e92-a112-b83691805155; __utma=115962011.1553833174.1346687005.1346687005.1346687005.1; __utmb=115962011.4.10.1346687005; __utmz=115962011.1346687005.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=115962011',
'HTTP_HOST': 'example.com',
'HTTP_REFERER': 'http://example.com/',
'HTTP_USER_AGENT': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64;  Trident/5.0)',
'PATH_INFO': u'/login.html',
'PATH_TRANSLATED': None,
'QUERY_STRING': None,
'REMOTE_ADDR': 'xx.xx.xx.xx',
'REMOTE_HOST': None,
'REMOTE_IDENT': None,
'REMOTE_USER': None,
'REQUEST_METHOD': 'POST',
'SCRIPT_NAME': '',
'SERVER_NAME': 'example.com',
'SERVER_PORT': 443,
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SOFTWARE': 'mod_python'}>

Why is the post dict has "<could not parse>" and why is CONTENT_TYPE None? This prevents me from crossing the login page.

This is my form,

   <form method="POST">{% csrf_token %}
       <tr>
     <td><label>Email Id</label></td>
     <td>{{form.username}}</td>
     <td><label>Password</label></td>
     <td>{{form.password}}</td>
   </tr>
    <input type="submit" name="btn_login" id="btn_login" class="btn_login" value="Login" /></td>

  </form>

Forms.py:

class loginForm(ModelForm):
    username = forms.CharField(max_length=50)
    password = forms.CharField(max_length=50, widget=forms.PasswordInput(attrs=  {"autocomplete":"off"}))

    class Meta:
        model = User

View:

def login(request):
 if request.method == "POST":
   log.inof(request)
 else:
  request.session.set_test_cookie()
  form = loginForm()
  return render_to_response('login.html', {'form':form},context_instance=RequestContext(request))

NOTE: I also found that all requests are coming from only mobile devices especially Blackberry. Seems one more user has the same problem

REF: https://stackoverflow.com/questions/12471661/mod-python-could-not-parse-the-django-post-request-for-blackberry-and-some-andro

解决方案

Since the error doesn't occur on your development stack, just sometimes on production I doubt it is something in your Django code.

How are you contacting the machine? The requests is going to localhost.. Are you running a browser on your production machine or is there some proxy or load balancer in front of it?

It appears to me that something is mangling your requests before they hit mod_python, be it a proxy or misconfigured ssl.

The symptom is you receive broken requests. The problem could be anywhere from your python app down all OSI layers on your end, up all OSI layers on the clients end. Try to make full picture of all things in between and systematically rule them out.

Your TCP/IP stack seems to work fine, or you would see other requests and services suffer too. So we can rule out everything below the Session Layer. That leaves the Presentation Layer (SSL) and the Application Layer (HTTP).

  1. SSL

    SERVER_PORT is 443, SSL is in play here. The request headers (HTTP_REFERER etc.) contain valid strings, so SSL seems to work fine. But I've seen some weirdly messed up TLS sessions before.

  2. HTTP

    The players at this layer:

    • your Apache httpd
    • any reverse proxies on your end
    • any forward proxies between you and client. Are REMOTE_ADDR all in the same subnet, is there some mobile operator out there with broken proxies?
    • the client user agent. Are all blackberries broken? Try to get your hands on the browser you see failing most.

And at what layer are your loadbalancers operating?

Seems the main problem is a missing Content-Type header on the POST request, and hence a value on the Python representation of the POSTed content that can't be pretty printed using pprint, let alone used by your app.

Disregarding what I found in the Blackberry developer ref: have you tried setting enctype explicitly as Simon suggested? Try varying application/x-www-form-urlencoded and multipart/form-data.

Best of all try to reproduce it yourself. Try making a tcpdump that records such a request. You can analyse a recorded dump with wireshark.

这篇关于django request.POST包含&lt;无法解析&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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