2012-11-09 78 views
-3

我对这种行为感到困惑。代码只是打印请求的类型。第一种方法有效,但第二种方法返回空字符串。该类的类型是django.corehandlers.wsgi.WSGIRequest。Django - 请求类型


(编辑澄清)

def someview(request):                                   
    html+="<p>type of the request each char "                             
    for i in range(47):                                  
     html+= (str(type(request))[i])                               
    html+="</p>"                                    

    html+="<p>type of the request each char "                             
    for i in range(47):                                  
     html+= repr(str(type(request))[i])                              
    html+="</p>"                                    

    return HttpResponse(html) 

结果如下。第二个字符串是空的。

type of the request each char '<''c''l''a''s''s'' '"'"'d''j''a''n''g''o''.''c''o''r''e''.''h''a''n''d''l''e''r''s''.''w''s''g''i''.''W''S''G''I''R''e''q''u''e''s''t'"'"'>' 

type of the request: 
+0

你想在这里做什么?类型(请求)应该给你''左右。如果你拿这个len():18个字符。那么你如何得到47?你传递给函数的是什么?你的目标是什么? –

+0

你想拥有“?”吗?符号由'str(type(request))'评估结果替换? –

回答

1

所以你的问题是为什么第二行不打印任何东西?答案是你需要repr(),而不是str()

但是,你意识到它会总是是相同的WSGIRequest,对不对?您正在打印request对象的类别的表示形式。您可能想要request.METHOD

+0

谢谢。为什么str()不起作用?我会认为str(type(X))总是会返回一些东西。 – RParadox