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!!!
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)
{% 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 %}
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")
]
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.