获取输入值并将其显示在其他模板中

时间:2019-03-14 13:35:22

标签: python django web

对不起,我刚刚学习编程

我有注册模板和注册成功模板。

用户填写表格注册后,它将重定向到注册成功模板

如何从注册模板中获取输入值['email']并将其显示在注册成功模板中?

views.py

class RegisterFormView(TemplateView):
template_name = 'home/register.html'

def get(self, request):
    user_form = UserForm()
    return self.render_to_response({
        'base_url' : settings.BASE_URL,
        'user_form': user_form,

    })

def post(self, request, *args, **kwargs):
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        if user_form.is_valid():
            user = user_form.save()
            user.full_name = user_form.cleaned_data.get("full_name")
            user.email = user_form.cleaned_data.get("email")
            user.phone_number = user_form.cleaned_data.get("phone_number")
            user.set_password(user_form.cleaned_data.get("password"))
            password = user_form.cleaned_data.get("password")
            profile = Profile.objects.create(created_by=user)
            user.save()
            return redirect(reverse("home:register-success"))
        else:
            user_form = UserForm(request.POST)
    return self.render_to_response({
        'base_url' : settings.BASE_URL,
        'user_form': user_form,
    })

register.html

{{user_form|crispy}}

register-success.html

we have send email activation to {{user.email}}

编辑

注册views.py

class RegisterSuccessView(TemplateView):

template_name = 'home/register-success.html'

def get(self, request):
    return self.render_to_response({
        'base_url' : settings.BASE_URL,
    })

1 个答案:

答案 0 :(得分:3)

请尝试替换

return redirect(reverse("home:register-success"))

return render(self.request, template_name='home/register-success.html', context={'user': user})

如果您想为register-success.html使用另一个URL,则可以创建一个包含页面和上下文返回的附加视图