Django Registration and Authenticate

Екатерина Замятина

I want to make a registration and authenticate on the one page.

If enter correct data in the first form (form1 - authenticate) than nothing will happen.

If enter data in the second form (form2 - registration) than nothing will happen too. But i don't see why ??!!

It's my first prodject, so...

If you help me, I will be very-very-very glad!!!

views.py

class RegisterFormView(FormView):
    form_class = UserCreationForm
    def form_valid(self, form):
        form.save()
        return super(RegisterFormView, self).form_valid(form)

    def get_success_url(self):
        return resolve_url('core:mainPage')

def myview(request):
    form1 = UserForm(request.POST or None)
    form2 = UserCreationForm(request.POST or None)
    context = {'form1': form1, 'form2': form2,}
    if request.method == 'POST' and form1.is_valid():
        user = authenticate(username=form1.cleaned_data['user'], password=form1.cleaned_data['password'])
        if user is not None:
            if user.is_active:
                login(request, user)
                render(request, 'core/main_page.html')
            else:
                return render(request, 'core/main_page.html')
        else:
            return render(request, 'core/main_page.html')
    if request.method == 'POST' and form2.is_valid():
        RegisterFormView.form_valid(request, form2)
    return render(request, 'core/login.html', context)

login.html

{% extends "core/base.html" %}
{% load widget_tweaks %}

{% block title %} {% endblock %}
{% block content %}
    <div class="container header clearfix">

        <h3> Авторизация </h3>
        <form method="post" class="form-horizontal">
            {% csrf_token %}
            {% render_field form1.user class="form-control" placeholder="Логин"%}
            {% render_field form1.password class="form-control" placeholder="Пароль"%}
            {{ form.errors }}
            <input type="submit" class="btn btn-default" id="norm">
        </form>

        <h3>Регистрация</h3>
        <form method="post" class="form-horizontal" role="form">
            {% csrf_token %}
            {% render_field form2.first_name class="form-control" placeholder="Ваше имя"%}
            {% render_field form2.last_name class="form-control" placeholder="Ваша фамилия"%}
            {% render_field form2.username class="form-control" placeholder="Логин"%}
            {% render_field form2.password1 class="form-control" placeholder="Пароль"%}
            {% render_field form2.password2 class="form-control" placeholder="Повторите пароль"%}
            {% render_field form2.email class="form-control" placeholder="email"%}
            {% render_field form2.email class="form-control" placeholder="Повтотрите email"%}
            <input type="submit" class="btn btn-default" >
        </form>


{% endblock  %}

URLS

from django.conf.urls import url, include
from django.contrib.auth.views import login, logout
from . import views


urlpatterns = [
    url(r'^$', views.MainPage.as_view(), name="mainPage"),
    url(r'^support/', views.HowToPrint.as_view(), name="support"),
    url(r'^login/', views.myview, name="login"),
    url(r'^logout/', logout, name="logout"),
    url(r'^thank-you', views.Thanks.as_view(), name="thank-you")
]
deltaskelta

It appears here that you forgot to put a place for the form to post to (http://www.w3schools.com/tags/tag_form.asp)

If you read the link above you can see that the form looks like this...

<form action="demo_form.asp" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

demo_form.asp refers to the path that the form is sent to. In your template you need to add an action that will send it to your login URL. This is generally done by calling the name in the template action='{% url "login" %}' or you can directly refer to the URL by path.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django Registration Registration_form

From Dev

Django Registration or or django auth?

From Dev

Django Authenticate returns None

From Dev

How to authenticate ejabberd with django?

From Dev

Django custom registration form

From Dev

Django-registration not registering

From Dev

Django CSRF Error for Registration

From Dev

Django registration is not working

From Dev

How to subclass registration form in django-registration

From Dev

Django-registration, custom registration and validation

From Dev

Django Registration Redux custom registration form

From Dev

Django registration redux registration only with email (no password)

From Dev

Django-registration, custom registration and validation

From Dev

Django registration redux registration only with email (no password)

From Dev

Passport-local-mongoose : Authenticate user right after registration

From Dev

Passport-local-mongoose : Authenticate user right after registration

From Dev

SLL authenticate from apache in Django

From Dev

Can not authenticate using Django and ldap

From Dev

Django REST Framework TokenAuthentication to authenticate in Django generally

From Dev

Django REST Framework TokenAuthentication to authenticate in Django generally

From Dev

django rest framework user registration

From Dev

Applying bootstrap to a django registration page

From Dev

Registration Facebook user with Django Allauth

From Dev

Django rest framework user registration?

From Dev

smtpauthenticationerror 534 django python registration

From Dev

Django User Registration Form KeyError

From Dev

Django rest framework user registration?

From Dev

django rest framework user registration

From Dev

How to make django registration form

Related Related

HotTag

Archive