[英]Django How to add a logout successful message using the django.contrib.auth?
[英]How to add field to django.contrib.auth forms
我想向 django.contrib.auth forms 添加字段。 有沒有辦法做到這一點?
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
User = get_user_model()
client = Client(URL, API_KEY)
class UserChangeForm(forms.UserChangeForm):
class Meta(forms.UserChangeForm.Meta):
model = User
class UserCreationForm(forms.UserCreationForm):
error_message = forms.UserCreationForm.error_messages.update(
{
"duplicate_username": _(
"This username has already been taken."
)
}
)
class Meta(forms.UserCreationForm.Meta):
model = User
def clean_username(self):
username = self.cleaned_data["username"]
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise ValidationError(
self.error_messages["duplicate_username"]
)
Models.py [這是我的 models.py 文件]
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
class User(AbstractUser):
# First Name and Last Name Do Not Cover Name Patterns
# Around the Globe.
name = models.CharField(
_("Name of User"), blank=True, max_length=255
)
phone= models.CharField(
_("Phone"), blank=True, max_length=20
)
def get_absolute_url(self):
return reverse(
"users:detail", kwargs={"username": self.username}
)
Usercreationform 基本上是 ACCOUNT_FORMS 注冊,我正在使用 django allauth forms 但我不知道如何向其中添加字段,例如電話號碼。
To add a field into your form, you'd have to add the same field in the model, because that form is creating a new object from the django.contrib.auth
User model. 您可以通過多種方式實現此目的,最簡單的方法是創建一個新用戶 model,該用戶的 ForeignKey 指向 Django 之一。 我建議您查看這個問題或有關此主題的 官方 Django 文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.