2011-06-24 9 views
1

私は、ビューにログインしている:ジャンゴ - %のためのサポートされていないオペランドのタイプ(S): 'HttpResponseRedirect' と 'ユニコード'

def login_view(request): 
    username = request.POST['username'] 
    password = request.POST['password'] 
    user = authenticate(username=username, password=password) 
    if user is not None: 
     if user.is_active: 
      login(request, user) 
      HttpResponseRedirect('/profile/%s/'%username) 
     else: 
      return HttpResponse("Aktywacja konta nie została zakończona") 
    else: 
     return HttpResponse('invalid login') 

urls.py

url(r'^login/', 
    "social.views.login_view", 
    name='login_view'), 

テンプレート:

<form method="post" action="{% url login_view %}">{% csrf_token %} 
    <p><label for="id_username">Login:</label> <input id="id_username" type="text" name="username" maxlength="30" /></p> 
    <p><label for="id_password">Hasło:</label> <input type="password" name="password" id="id_password" /></p> 
    <input type="submit" value="Zaloguj" /> 
    <input type="hidden" name="next" value="" /> 
    </form> 

しかし、私は得る:

TypeError at /accounts/login/ 
unsupported operand type(s) for %: 'HttpResponseRedirect' and 'unicode' 

str()関数は役に立ちません。すべてのユーザー名にはASCII文字しか含めることができません。誰かがこれを修正する方法を知っていますか?

+0

HttpResponseRedirectを返さないでください。そして、エラーは 'HttpResponseRedirect 'の前に'% 'の前に最後のparenがあったためです。受け入れられた答えは何も変わらないでしょう、ちょうどあなたが正しい場所にparenを置くようにしました。 – sdolan

答えて

1

後でHttpResponseRedirectに与える変数に文字列を格納しないでください。

foo = "/profile/%s/" % username 
HttpResponseRedirect(foo) 
+0

私はそれが助けることができることを知っていませんが、助けになるので。ありがとう。 – krzyhub

+0

あなたは大歓迎です:) – Oleiade

関連する問題