A
A
alexkomp2019-08-26 13:07:33
Django
alexkomp, 2019-08-26 13:07:33

How to make the registration and authorization forms on the same page of the site?

#функция для главной страницы
def shop_ware(request):
    #форма регистрации
    form = SignupForm(request.POST)
    if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            mail_subject = 'Activate your blog account.'
            message = render_to_string('acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid':urlsafe_base64_encode(force_bytes(user.pk)),
                'token':account_activation_token.make_token(user),
            })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(
                        mail_subject, message, to=[to_email]
            )
            email.send()
            return render(request, 'send.html')
    else:
        form = SignupForm() 
    
    context ={
        'form': form
    }
    return render(request, 'index.html', context)


#форма авторизвции
class LoginFormView(FormView):
    form_class = AuthenticationForm

    # Аналогично регистрации, только используем шаблон аутентификации.
    template_name = "login.html"

    # В случае успеха перенаправим на главную.
    success_url = "/"

    def form_valid(self, form):
        # Получаем объект пользователя на основе введённых в форму данных.
        self.user = form.get_user()

        # Выполняем аутентификацию пользователя.
        login(self.request, self.user)
        return super(LoginFormView, self).form_valid(form)

How to make the authorization form on the same page as the registration form.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-08-26
@alexkomp

First link on Google:
https://stackoverflow.com/a/45599302/2981702

spoiler
<!-- Sign In Form -->
<form>
   <button type='submit' name='submit' value='sign_in'></button>
</form>
<!-- Sign Up Form -->
<form>
   <button type='submit' name='submit' value='sign_up'></button>
</form>

#views.py
def index(request):
    if request.method == "POST":
        if request.POST.get('submit') == 'sign_in':
            # your sign in logic goes here
        elif request.POST.get('submit') == 'sign_up':
            # your sign up logic goes here

PS You already decide whether you have CBV or FBV.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question