Django:从密码重置电子邮件中检索POST参数

时间:2018-01-27 11:32:28

标签: python django django-forms django-templates django-2.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检索它,此页面的网址如下http://127.0.0.1:8000/accounts/password_reset/?origin_page=/mypage/

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

1 个答案:

答案 0 :(得分: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:&#39; \

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)