【发布时间】:2019-12-08 23:19:29
【问题描述】:
我是 Django 新手,我正在尝试对我的自定义用户模型进行用户身份验证。我的模型创建成功,'createsuperuser' 命令在其中插入新用户。然后我可以使用这些帐户登录,一切正常。但我希望能够从我的注册表单中插入新用户。我遵循了 Django 文档中的步骤,但是,它没有插入新用户。它没有给我任何错误,因此我将不胜感激。
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
}
]
forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import User
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User
fields = ('username', 'full_name', 'country', 'city', 'birthday', 'language', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = User
fields = UserChangeForm.Meta.fields
views.py
from django.views.generic.edit import CreateView
from .forms import CustomUserCreationForm
class Register(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'movies_app/register.html'
movie_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies_app/', include('django.contrib.auth.urls')),
path('', include('movies_app.urls', namespace='movies_app')),
]
movies_app/urls.py
from django.urls import path
from . import views
from .views import Register
app_name = 'movies_app'
urlpatterns = [
path('', views.index, name='index'),
path('register/', Register.as_view(), name='Register')
]
【问题讨论】: