【问题标题】:Django - how to send "POST" information from one view to anotherDjango - 如何将“POST”信息从一个视图发送到另一个视图
【发布时间】:2014-02-06 05:50:12
【问题描述】:

我有一个注册表,如果注册表填写正确且有效,我想自动登录用户。登录视图在 URL 处

/login/

这是该 URL 链接到的登录视图(它是通用登录视图):

def login(request, template_name='registration/login.html',
      redirect_field_name=REDIRECT_FIELD_NAME,
      authentication_form=AuthenticationForm,
      current_app=None, extra_context=None):

    if request.method == "POST":
        form = authentication_form(data=request.POST)
        if form.is_valid():

            # Ensure the user-originating redirection url is safe.
            if not is_safe_url(url=redirect_to, host=request.get_host()):
                redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

            # Okay, security check complete. Log the user in.
            auth_login(request, form.get_user())

            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()

            return HttpResponseRedirect(redirect_to)

else:
    form = authentication_form(request)

request.session.set_test_cookie()

current_site = get_current_site(request)

context = {
    'form': form,
    redirect_field_name: redirect_to,
    'site': current_site,
    'site_name': current_site.name,
}
if extra_context is not None:
    context.update(extra_context)
return TemplateResponse(request, template_name, context,
                        current_app=current_app)

现在,处理注册的视图是:

def registrationView(request):
    if request.method == 'POST':
    form = RegistrationForm(request.POST)

    if form.is_valid():
        user = User.objects.create_user(
        username=form.clean_data['username'],
        password=form.clean_data['password1'],
        email=form.clean_data['email']
        )
        return HttpResponseRedirect('/login/')

现在,当我重定向到

/login/

在注册视图中,我是否可以将用户名和密码作为“POST”信息发送,以便当它到达登录视图时

if request.method == "POST":

评估为真?

【问题讨论】:

  • 你为什么要做那件事?
  • @geekazoid 因为我想使用登录视图来登录用户。

标签: django django-authentication django-registration http-redirect django-login


【解决方案1】:

我认为,与其尝试做这种魔术,不如将当前用户设置为您的会话。

def registrationView(request):
    if request.method == 'POST':
    form = RegistrationForm(request.POST)

    if form.is_valid():
        user = User.objects.create_user(
        username=form.cleaned_data['username'],
        password=form.cleaned_data['password1'],
        email=form.cleaned_data['email']
        )
        login(request, user) 

        return HttpResponseRedirect('...')

看看https://docs.djangoproject.com/en/1.5/topics/auth/default/#django.contrib.auth.login

【讨论】:

  • 嗯,有道理。当我使用你的代码时,它给出了一个错误,说“'RegistrationForm'对象没有属性'clean_data'”并追溯到registrationView中的“username = form.clean_data ['username']”行。知道为什么?
  • @sk1p 是的,刚刚注意到,谢谢,解决了问题。
  • @user2719875 您是否修改了您的TEMPLATE_CONTEXT_PROCESSORS 设置?它是否仍然包含auth 上下文处理器?您是否尝试将{{ user }}{{ user.username }} 放入您的模板中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多