Created
February 12, 2018 06:38
-
-
Save dura0ok/479c7170046409da298527716a376bc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% block content %} | |
<form action="" method="post"> | |
{% csrf_token %} | |
<!-- as_p для того, чтобы каждый элемент формы был с новой строки --> | |
{{ form.as_p }} | |
<button type="submit">Регистрация</button> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url | |
from . import views | |
#main urls | |
#urls views.py STEPAN :D | |
urlpatterns = [ | |
url(r'^login/$', views.LoginFormView.as_view()), | |
url(r'^register/$', views.RegisterFormView.as_view()), | |
url(r'^logout/$', views.LogoutView.as_view()), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
# Create your views here. | |
from django.views.generic.edit import FormView | |
from django.contrib.auth.forms import UserCreationForm | |
# Опять же, спасибо django за готовую форму аутентификации. | |
from django.contrib.auth.forms import AuthenticationForm | |
from django.http import HttpResponseRedirect | |
from django.views.generic.base import View | |
from django.contrib.auth import logout | |
# Функция для установки сессионного ключа. | |
# По нему django будет определять, выполнил ли вход пользователь. | |
from django.contrib.auth import login | |
class RegisterFormView(FormView): | |
form_class = UserCreationForm | |
# Ссылка, на которую будет перенаправляться пользователь в случае успешной регистрации. | |
# В данном случае указана ссылка на страницу входа для зарегистрированных пользователей. | |
success_url = "/login/" | |
# Шаблон, который будет использоваться при отображении представления. | |
template_name = "register.html" | |
def form_valid(self, form): | |
# Создаём пользователя, если данные в форму были введены корректно. | |
form.save() | |
# Вызываем метод базового класса | |
return super(RegisterFormView, self).form_valid(form) | |
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) | |
class LogoutView(View): | |
def get(self, request): | |
# Выполняем выход для пользователя, запросившего данное представление. | |
logout(request) | |
# После чего, перенаправляем пользователя на главную страницу. | |
return HttpResponseRedirect("/") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url, include | |
from . import views | |
from django.conf import settings | |
from django.conf.urls.static import static | |
urlpatterns = [ | |
url(r'^$', views.post_list, name='post_list'), | |
url(r'^post/(?P<id>[0-9]+)$', views.getfull, name='full'), | |
url(r'^add$', views.post_new, name='post_new'), | |
url(r'^post/(?P<pk>\d+)/edit/$', views.post_edit, name='post_edit'), | |
url(r'^', include('users.urls')), | |
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment