1

次のようなURLに送信するようにアプリを正常に設定しました

http://localhost:8000/log_in/?next=/task/1/

私はコンテキストプロセッサを次のように定義しました

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request",

)

しかし、ログインすると、代わりにホームページにリダイレクトされrequestます。ログインビューにを印刷すると、GET空のクエリセットが取得されました。これは、ストランドです。理由は何ですか。

私も追加してみました

<input type="hidden" name="next" value="{{next}}" />

login.htmlに追加されましたが、これPOSTによりリクエストの横に空のパラメータが追加されました

編集1 ログインビューで、「next」パラメータを出力しようとすると、GETは空のクエリセットであるため、nullが返されます。

def log_in(request):
    """
    This method checks for the input username(email) and password.
    If they are empty the user id redirected to the login page, else checks whether
    the user is authenticated. If the user is not found in the database, it renders the login
    page with an error_message that the email or password are invalid. Further, it checks if the user's
    account is activated, it not he is redirected back to login and notified.
    If the user is activated, it checks for the field remember_me, if it is not checked, then the
    expiry date of the session is set to when the browser is closed.
    Then the user is logged in using the login in django backends and it checks on the session
    value if it is his first time or not to open the getstrated instead of the normal profile.
    """
    print request.GET['next']
    base_login=True
    if request.POST.has_key('username') and request.POST.has_key('password'):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is None:
            error_message = "The Email/Password is incorrect."
            return render(request, 'userprofiles/registration.html', {'error_message': error_message})
        else:
            if user.is_active:
                if not request.POST.get('remember_me', False):
                    request.session.set_expiry(0)

                login(request, user)

                request.session['DEPLOYED_ADDRESS'] = settings.DEPLOYED_ADDRESS

                first_week = False
                logged_user = UserProfile.objects.get(username=request.user.username)
                if (((request.user.last_login - request.user.date_joined).days) < 8) and (logged_user.got_started == False):
                    first_week = True

                if first_week:
                    return redirect('/getting_started/', RequestContext(request))
                else:
                    return redirect('/', RequestContext(request))

            else:
                error_message = "This Email Hasn't Been Activated Yet. Please Check The Activation Mail Sent To You."
                return render(request, 'userprofiles/registration.html', {'error_message': error_message})
    else:
        RegistrationForm = get_form_class(up_settings.REGISTRATION_FORM)

        if request.method == 'POST':
            form = RegistrationForm(data=request.POST, files=request.FILES)

            if form.is_valid():
                new_user = form.save()
                username = form.cleaned_data['username']
                password = form.cleaned_data['password']

                request.session['login'] = "first"



                # Automatically log this user in
                if up_settings.AUTO_LOGIN:

                    if up_settings.EMAIL_ONLY:
                        username = form.cleaned_data['email']

                    user = authenticate(username=username, password=password)
                    if user is not None:
                        if user.is_active:
                            login(request, user)
                            calculate_profile_completion(request)


                return redirect(up_settings.REGISTRATION_REDIRECT)

        else:
            form = RegistrationForm()


        return render(request, 'userprofiles/registration.html', {'form': form,'base_login': base_login})
4

1 に答える 1

6

フォームをレンダリングする場合、ブラウザーはDjangoアプリにGETリクエストを送信します(これはととは関係ありませんrequest.GETrequest.POSTこれらはそれぞれURLパラメーターまたはフォームエンコードデータとして送信されたデータを含み、常に利用可能です)。GETリクエスト中にURLパラメータを使用しているため、nextその時点でURLパラメータにアクセスできます。ただし、その後、どこにも含めずにリクエストへの応答としてフォームページのレンダリングに進むため、nextブラウザはこの値にアクセスできなくなり、アプリもアクセスできなくなります。

その後、ブラウザがフォームの送信に進むと、POSTリクエストを実行します。このリクエストを処理するために同じハンドラーを使用している場合がありますが、それでも別のリクエストであるため、nextフォームまたはURLパラメーターを渡していないが失われます。

これは、元のGETリクエストで修正できます(ユーザーにフォームを表示する前に)。URLパラメータを受け取るとすぐnextに、次のいずれかを実行できます。

コンテキストを介して値をテンプレートに渡し、フォーム内に非表示フィールドをレンダリングして、ログインフォームに入力します。

<!-- inside your FORM element -->
<input type="hidden" name="next" value="{{ next }}">

GETリクエスト中に次のセッションを開始します。

request.session['next'] = request.GET.get('next', '/')

他にもメソッドがありますが(たとえば、ログインフォームクラスに非表示フィールドを追加する)、上記の2つが最も簡単です。

于 2013-01-08T14:47:46.687 に答える