2016-10-22 38 views
0

私はdjango 1.10埋め込み認証を使用しています。私は別のページへのログインリダイレクトと変数も渡したい。 Iveは私の意見を作りましたが、私はすべての時間エラー404を持っています。私は "login-redirect-url"について読んで、デフォルトはaccounts/profileです。私は自分のテンプレートの中にフォルダを作った。これは正しい方法ですか?私はDjango/myapp/myapp/templates/accounts/profile/home.htmlに保存します。ログイン後別のページにリダイレクト

エラー: "現在のアカウント、プロフィール/ home.htmlはこれらのいずれにも一致しませんでした。

views.py 

    def home(request): 
     username = None 
     if request.user.is_authenticated: 
      username = request.user.username 
      return redirect('accounts/profile/home.html',{'username': username}) 
     else: 
      return redirect('/accounts/profile') 


myapp/ulrs.py 

    url(r'^accounts/profile/$', views.home, name='home') 

私が何か提案がイムこの間違っをwelcome.Ifことになるだろうsettings.py

LOGIN_REDIRECT_URL = '/' 

を変えてくれ例を提供してください。

ありがとうございました。

答えて

0

uは要求をレンダリングするには、ユーザー名を渡す必要がいけません。

view.py

def profile(request): 
    username = None 
    if request.user.is_staff: 
     return redirect('/admin/') 
    else: 
     return render(request, 'accounts/profile/home.html',{}) 

home.html(myappの/ myappに/テンプレート/ myappに/ home.htmlそれを格納)

Hello user:{{request.user.username}} 
0

accounts/profile/home.htmlテンプレートを{'username': username}というコンテキストでレンダリングしたいとします。

return render(request, 'accounts/profile/home.html', context={'username': username}) 
+0

助けてくれてありがとう 私はしようとしましたが、まだeror 404があります。どこで家を建てたらいいですか? ありがとう – OverHeat

1

Dariusz Niespodzianyからの回答は有効ですが、テンプレートを変更することも有効です。次に、あなたのテンプレート(home.html)であなたはこのようなものを行うことができます

def home(request): 
     return redirect('/accounts/profile') 

:あなたのビューは次のようになります

{% if request.user.is_authenticated %} 
    <h1> Hi {{request.user.username}} </h1> 
{% else %} 
    <h1> Hi stranger </h1> 
{% endif %} 
+0

私は私が私の家を保存する必要がありますPLZを教えてくれるしようとしました。 html 私は設定を編集しました – OverHeat

+0

あなたのhome.htmlは 'templates'フォルダ内のアプリ内にある必要があります。それがなければ作成してください。 – 1GDST

0

これを試してみてください:ここで は一例です -

def user_login(request): 
    context = RequestContext(request) 
    if request.method == 'POST': 
     username = request.POST['login'] 
     password = request.POST['password'] 
     user = authenticate (email=username, password=password) 
     if user is not None: 
      if user.is_active: 
       login(request, user, backend='django.contrib.auth.backends.ModelBackend') 
       redirect_to = settings.LOGIN_REDIRECT_URL 
       return redirect(redirect_to,{your varible }) 
関連する問題