это код:
registration/urls.py:
Код: Выделить всё
from django.contrib.auth import views as auth_views
from django.urls import path
from . import views
urlpatterns = [
path('/register', views.register, name='register'),
]
Код: Выделить всё
from django.shortcuts import render
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from .forms import RegistrationForm
from django.shortcuts import render, redirect
def register(request): # Change function name to match the URL pattern
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
print(form)
return redirect('home')
else:
form = RegistrationForm()
return redirect('news')
return render(request, 'registration/register.html', {'form':form})
Код: Выделить всё
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('news/', include('news.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Код: Выделить всё
Регистрация
Регистрация
{% csrf_token %}
{{ form.email }}
{{ form.password1 }}
{{ form.password2 }}
Зарегистрироваться
Код: Выделить всё
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class RegistrationForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['email'].label = 'Email address'
self.fields['password1'].label = 'Password'
self.fields['password2'].label = 'Confirm Password'
class Meta:
model = User
fields = ('email', 'password1', 'password2')
Подробнее здесь: https://stackoverflow.com/questions/785 ... e-database