Commit c97964de authored by Борис Ким's avatar Борис Ким

Создал вью для удобной регистрации студентов

parent 1d482920
from django import forms from django import forms
from django.contrib.auth.models import User
class RegisterForm(forms.ModelForm):
password = forms.CharField(
label=('Password'),
required=True,
strip=False,
widget=forms.PasswordInput(attrs={'placeholder': 'Write password'})
)
password_confirm = forms.CharField(
label=("Confirm password"),
required=True,
strip=False,
widget=forms.PasswordInput(attrs={'placeholder': 'Confirm password'})
)
def clean(self):
cleaned_data = super().clean()
password = cleaned_data.get('password')
password_confirm = cleaned_data.get('password_confirm')
if password and password_confirm and password_confirm != password_confirm:
raise forms.ValidationError("Passwords are not equal")
return cleaned_data
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password'])
if commit:
user.save()
return user
class Meta:
model = User
fields = [
'username',
'first_name', 'last_name',
'email',
'password', 'password_confirm'
]
\ No newline at end of file
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
<input type="password" name="password" class="form-control" id="inputPassword3" placeholder="Password"> <input type="password" name="password" class="form-control" id="inputPassword3" placeholder="Password">
</div> </div>
</div> </div>
<input type="hidden" name="next" value="{{ request.GET.next }}">
<div class="form-group row"> <div class="form-group row">
<div class="col-sm-10"> <div class="col-sm-10">
<button class="btn btn-primary">Sign in</button> <button class="btn btn-primary">Sign in</button>
......
{% extends 'acc_base.html' %}
{% load crispy_forms_filters %}
{% block title %} University {% endblock %}
{% block content %}
<div class="container" style="margin: 50px auto;">
<div class="d-flex justify-content-center shadow-lg p-3 mb-5 bg-white rounded">
<form method="POST" action="{% url 'accounts:register' %}">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary"> Sign Up </button>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
...@@ -8,4 +8,5 @@ app_name = "accounts" ...@@ -8,4 +8,5 @@ app_name = "accounts"
urlpatterns = [ urlpatterns = [
path('login/', accounts_views.LoginView.as_view(), name="login"), path('login/', accounts_views.LoginView.as_view(), name="login"),
path('logout/', accounts_views.LogoutView.as_view(), name="logout"), path('logout/', accounts_views.LogoutView.as_view(), name="logout"),
path('register/', accounts_views.RegisterView.as_view(), name="register")
] ]
...@@ -2,7 +2,10 @@ from django.conf import settings ...@@ -2,7 +2,10 @@ from django.conf import settings
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.views.generic import View, FormView from django.views.generic import View, CreateView
from django.contrib.auth.models import User
from accounts.forms import RegisterForm
class LoginView(View): class LoginView(View):
...@@ -26,4 +29,16 @@ class LoginView(View): ...@@ -26,4 +29,16 @@ class LoginView(View):
class LogoutView(View): class LogoutView(View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
logout(request) logout(request)
return redirect(settings.LOGOUT_REDIRECT_URL) return redirect(settings.LOGOUT_REDIRECT_URL)
\ No newline at end of file
class RegisterView(CreateView):
model = User
template_name = 'auth/register.html'
form_class = RegisterForm
success_url = '/'
def form_valid(self, form):
user = form.save()
login(self.request, user)
return redirect("webapp:courses_list")
{% load static%}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css">
<link href="{% static 'fontawesomefree/css/all.min.css' %}" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<title>{% block title %} University {% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
\ No newline at end of file
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
</div> </div>
{% else %} {% else %}
<div> <div>
<a href="{% url 'accounts:login' %}">Sign in</a> <a href="{% url 'accounts:login' %}?next={{ request.get_full_path }}">Sign in</a>
</div> </div>
{% endif %} {% endif %}
</nav> </nav>
......
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.urls.base import reverse, reverse_lazy from django.urls.base import reverse, reverse_lazy
from django.views.generic import CreateView, DetailView, DeleteView from django.views.generic import CreateView, DeleteView, UpdateView
from django.views.generic import UpdateView from django.contrib.auth.mixins import LoginRequiredMixin
from webapp.models import Class, Course from webapp.models import Class, Course
from webapp.forms import ClassForm from webapp.forms import ClassForm
class CreateClassView(CreateView): class CreateClassView(LoginRequiredMixin ,CreateView):
model = Class model = Class
fields = ['title', 'description', 'file'] fields = ['title', 'description', 'file']
template_name = 'courses/create.html' template_name = 'courses/create.html'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment