39

リファラーページへのリダイレクトを除いて、正常に機能しているログインページがあります。ユーザーは、アプリ内の直接リンクが記載された電子メールを受け取ります。ユーザー (この例では) はまだログインしておらず、ログイン ページにリダイレクトされます。ログインに成功すると、ユーザーはハードコーディングされたパスにリダイレクトされます。以下の例を参照してください。

メールの URL:http://localhost:8000/issueapp/1628/view/22

ログインページの URL:http://localhost:8000/login?next=/issueapp/1628/view/22

ログイン ビュー (ハードコーディングされたリダイレクトあり):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.POST:
        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)
                state = "You're successfully logged in!"
                return HttpResponseRedirect('/issueapp/1628/')
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

ログイン ビュー (「次の」リダイレクトあり):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.POST:
        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)
                state = "You're successfully logged in!"
                return HttpResponseRedirect(request.GET['next'])
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

上記のビューでは例外が発生します"Key 'next' not found in <QueryDict: {}>"。フォームは、URL とフォームにあるにもかかわらず、「次の」変数を投稿しているようには見えません。私はどこでも検索して見ましたが、なぜ機能しないのかわかりません。何か案は?

追加の参照:

ログイン テンプレート:

{% block content %}

    {{ state }}
    <form action="/login/" method="post" >
                {% csrf_token %}
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        username:
        <input type="text" name="username" value="{{ username }}" /><br />
        password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In"/>

        {% debug %}
    </form>
{% endblock %}

編集: 以下は、現在私のために機能しているコードです (Paulo Bu の助けのおかげで)! **

ログイン ビュー:

def login_user(request):

    state = "Please log in below..."
    username = password = ''

    next = ""

    if request.GET:  
        next = request.GET['next']

    if request.POST:
        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)
                state = "You're successfully logged in!"
                if next == "":
                    return HttpResponseRedirect('/issueapp/')
                else:
                    return HttpResponseRedirect(next)
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username,
        'next':next,
        },
        context_instance=RequestContext(request)
    )

ログイン テンプレート:

{{ state }}

{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}

            {% csrf_token %}

    username:
    <input type="text" name="username" value="{{ username }}" /><br />
    password:
    <input type="password" name="password" value="" /><br />

    <input type="submit" value="Log In"/>

    {% debug %}
</form>
4

5 に答える 5

6

要するに

ビュー関数next_page = request.GET['next']で定義してからリダイレクトするreturn HttpResponseRedirect(next_page)ので、テンプレートを変更する必要はありません。設定@login_requiredするだけで大​​丈夫です。

例として:

ユーザー A は、ログインしていない状態でhttps://www.domain.tld/account/にアクセスしようとします。@login_requiredが settings.pyで定義されているため、Django は彼をリダイレクトしLOGIN_URLます。ユーザー A が正常にログインすると、このメソッドはパラメータをUserLogin試行しGET、それにリダイレクトします。next

設定.py

LOGIN_URL = '/login/'

urls.py

url(r'^account/', account, name='account'),
url(r'^login/$', UserLogin, name='login'),

ビュー.py

@login_required
def account(request):
    return HttpResponseRedirect("https://www.domain.tld/example-redirect/")

def UserLogin(request):
    next_page = request.GET['next']
    if request.user.is_authenticated():
        return HttpResponseRedirect(next_page)
    else:
        if request.method == 'POST':
            if form.is_valid():
                username = form.cleaned_data['username']
                password = form.cleaned_data['password']
                user = authenticate(email=username, password=password)
                if user is not None and user.is_active:
                    login(request, user)
                    return HttpResponseRedirect(next_page)
                else:
                    error_msg = 'There was an error!'
                    return render(request, "login", {'form': form, 'error_msg': error_msg})
            else:
                error_msg = "There was an error!"
                return render(request, "login", {'form':form, 'error_msg':error_msg})
        else:
            form = UserLoginForm()
            return render(request, "login", {'form': form})
于 2016-03-01T16:56:08.280 に答える
4

置くだけ

<form action="" method="post" >

空のアクション ' what ever current complete url is'

于 2015-11-19T11:42:57.627 に答える
3

より一般的にしたい場合は、フォームが投稿されたときに GET パラメーターのいずれかを渡す、次のようなことを行うことができます。

<form action="/path-to-whatever/{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}" method="post">
于 2014-09-05T15:40:34.710 に答える