为什么login()函数正在循环?

时间:2019-05-21 15:28:33

标签: django django-views

用户registration需要登录并重定向到配置文件之后。还应该有手动输入的逻辑。为此,我尝试覆盖login()函数。在第一种情况下,应该会自动登录并重定向到您的个人资料。

但是login()函数正在循环。

from django.contrib.auth import authenticate, login as auth_login

def registration(request):
    if request.user.is_authenticated():
        return redirect(request.user.get_absolute_url())
    else:
        if request.method == "POST":
            form = RegistrationForm(data=request.POST)
            if form.is_valid():
                cleaned_data = form.cleaned_data
                password = User.objects.make_random_password()
                site = get_current_site(request)
                cleaned_data['username'] = cleaned_data['email']
                cleaned_data['password'] = password
                email = cleaned_data['email']
                new_user = RegistrationProfile.objects.create_inactive_user(
                    form=form, site=site, send_email=False,
                )
                new_user.set_password(password)
                new_user.first_name = cleaned_data['first_name']
                new_user.last_name = cleaned_data['last_name']
                new_user.slug = 'id' + str(new_user.id)
                new_user.save(update_fields=['first_name', 'last_name', 'password', 'slug'])
                registration_profile = RegistrationProfile.objects.get(
                    user=new_user)

                signals.user_registered.send(sender=None, user=new_user,
                                             request=request)
                if request.is_ajax():
                    user = authenticate(username=email, password=password,)
                    auth_login(request, user)
                    to = reverse('profile_view', args=[request.user.slug])
                    return JsonResponse({'success': True, 'location': to})
                else:
                    global user_
                    user_ = authenticate(username=email, password=password,)
                    auth_login(request, user_)
                    return HttpResponseRedirect(reverse('profile_view', args=[request.user.slug]))
            else:
                if request.is_ajax():
                    errors = dict([(k, force_text(v[0])) for k, v in
                                   form.errors.items()])
                    return JsonResponse({'success': False, 'errors': errors})
        else:
            return redirect('home')

def login(request, template_name='accounts/login.html',
          redirect_field_name=REDIRECT_FIELD_NAME,
          authentication_form=AuthenticationForm,
          extra_context=None):
      auth_login(request, user_)
      to = reverse('profile_view', args=[request.user.slug])
      #looping here
      return HttpResponseRedirect(reverse('profile_view', args=[request.user.slug]))
      form = authentication_form(request)

    return TemplateResponse(request, template_name, context)


@method_decorator(login_required, name='dispatch')
class ProfileView(DetailView):


LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = "/login/success/"```


0 个答案:

没有答案