0

我对 django 还很陌生,我目前正在重写 django 登录视图,因此我可以将所有身份验证过程移动到一个专用的 django 应用程序,在那里我可以重新定义模板。所以目前我有一个看起来像这样的accounts应用程序:url.py

from django.contrib.auth import views as auth_views
from django.urls import path
from . import views

app_name = 'accounts'
urlpatterns = [
    path('login/', auth_views.login, {'template_name': 'accounts/login.html'}, name='login'),
    path('logout/', auth_views.logout, name='logout'),
    path('password_reset/', auth_views.password_reset,
         {'template_name': 'accounts/password_reset_form.html',
          'email_template_name': 'accounts/password_reset_email.html',
          'subject_template_name': 'accounts/password_reset_subject.txt',
          'post_reset_redirect': 'done/'}, name='password_reset'),
    path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(
        template_name='accounts/password_reset_done.html'), name='password_reset_done'),

    path('reset/<uidb64>/<token>/', auth_views.password_reset_confirm,
         {'template_name': 'accounts/password_reset_confirm.html',
          'post_reset_redirect': '/accounts/reset/done/'}, name='password_reset_confirm'),
    path('reset/done/', auth_views.PasswordResetCompleteView.as_view(
        template_name='accounts/password_reset_complete.html'), name='password_reset_complete')
]

看起来像accounts/password_reset_email.html这样:

{% autoescape off %}
To initiate the password reset process for your {{ user.get_username }} account,
click the link below:

{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}?origin_page={{ request.POST.origin_page }}"

...

Sincerely,
The Team
{% endautoescape %}

我想要做的是恢复origin_page参数,因此当用户单击电子邮件中的重置链接时,他会在重置密码后重定向到正确的网页。到目前为止,我试图这样做password_reset_form.html

{% block content %}
    <p>Forgotten your password? Enter your email address below, and we'll email instructions for setting a new one.</p>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="hidden" name="origin_page" value="{{ request.GET.origin_page }}"/>
        <button type="submit">Submit</button>
    </form>
{% endblock %}

在这里,我只想将我origin_page的提交表单传递给我,以便我可以从中检索它accounts/password_reset_email.html,该页面的 url 看起来像这样http://127.0.0.1:8000/accounts/password_reset/?origin_page=/mypage/

如何从中恢复request.POST.origin_page参数accounts/password_reset_email.html?谢谢。

4

1 回答 1

1

也许这有帮助

path('password-reset/',
         auth_views.PasswordResetView.as_view(
            template_name='form.html',
            email_template_name='email.html',
            extra_email_context={
                'origin_page':'origin_page_thing'
            }
         ),
         name='password_reset'),

电子邮件:

{{ protocol }}://{{ domain }}{% url 'accounts:password_reset_confirm' uidb64=uid token=token %}?origin_page={{ origin_page }}

E-Kami 解决方案:

好的,我知道了,我实际上创建了一个新的 PasswordResetView,如下所示:'\

class PasswordResetView(auth_views.PasswordResetView):
    template_name = 'commons/password_reset_form.html'
    email_template_name = 'commons/password_reset_email.html'
    subject_template_name = 'commons/password_reset_subject.txt'
    success_url = 'done/'

    def post(self, request, *args, **kwargs):
        self.extra_email_context = {
            'origin_page': self.request.POST['origin_page']
        }
        return super().post(request, *args, **kwargs)
于 2018-01-27T13:42:15.870 回答