-3

私はこの行動に戸惑っています。コードは単にリクエストのタイプを出力します。最初のメソッドは機能しますが、2番目のメソッドは空の文字列を返します。クラスのタイプはdjango.corehandlers.wsgi.WSGIRequestです。


(明確にするために編集)

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) 

結果は次のようになります。2番目の文字列は空です。

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:   
4

1 に答える 1

1

では、なぜ2行目は何も印刷されないのでしょうか。その答えはrepr()、ではなく、が必要だということですstr()

ただし、それは常に同じWSGIRequestになることに気づきましたか?オブジェクトのクラスの表現を印刷していrequestます。

あなたはおそらく欲しいですrequest.METHOD

于 2012-11-10T11:15:39.910 に答える