I want the user to be able to update their username but not email address. In example 3 despite the fact I do not include the field email in my code, the field still appears when I run the site. Admittedly the text box is blank whereas in example 1 and 2 it is populated.
How can I stop the email text box appearing? Or can I lock it so the user cannot enter a new value?
Example 1
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email']
Example 2
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['email']
Example 3
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username']
from django.forms import ModelForm
class UserUpdateForm(ModelForm):
#email = forms.EmailField()
class Meta:
model = User
fields = ['username']
Since you need all other fields except email (or at least you mentioned only email as the one you'd like to hide), perhaps you can use then exclude attribute:
from django.forms import ModelForm
class UserUpdateForm(ModelForm):
class Meta:
model = User
exclude = ('email',)
For more details you can read in this doc.
Also, alternative way to go is to disable field (not checked):
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField(disabled=True)
class Meta:
model = User
fields = ['username', 'email']
Passing disabled=True should still render the field but unable it edition by user.
Related
I made a registration page in django but the problem is that it should not accept one email address for multiple accounts. How to resolve this issue? If you need code then let me know.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ['username','email','password1','password2']
No need of entering email in forms , User model already contains a email column
your registration form should look like this
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
and you can simply use this line in models.py to make email Unique
User._meta.get_field('email')._unique = True
I am currently trying to improve my knowledge in coding only by using class-based views. I am currently using Django 2.0.7 and I got a bit stuck. I was trying to extend the User model in order to create accounts. This was easily done. But I can't make the passwords to get hashed. Also, when I try to type, it will not be hidden even when using PasswordInput widget. Any advice ?
#models.py
class Client(User):
name = models.CharField(max_length=30)
surname = models.CharField(max_length=50)
phone = models.CharField(max_length=15,
validators=[
RegexValidator(
regex='^[0-9+]+',
message='Not a valid phone number.',
),
])
address = models.CharField(max_length=255)
class Meta:
verbose_name = 'Client'
#forms.py
class ClientForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
fields = ('username', 'password', 'email', 'name', 'surname', 'phone', 'address')
model = Client
#views.py
class HomeView(CreateView):
template_name = 'home.html'
model = Client
form = ClientForm
fields = ('username', 'password', 'email', 'name', 'surname', 'phone', 'address')
success_url = reverse_lazy('home')
After you created your own user model without password, use the set_password() method to assign a password to your user object.
Ex: your_object.set_password(<psswd>)
I have a custom user model that I need to add an extra field to. The problem I have is that the information needed is user input from the registration page.
This is the code from the form:
team = forms.ChoiceField(choices=teamChoices)
This is my user model:
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
I need to add something like this:
groups = 'team'
But I don't know how I would get that information to my model. My user model is in models.py and my registration form is in forms.py.
This is is my current Register Form but I haven't tested to see if it works yet:
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'placeholder':'First Name', 'class': 'form-input'}))
last_name = forms.CharField(max_length=30,widget=forms.TextInput(attrs={'placeholder':'Last Name', 'class': 'form-input'}))
email = forms.EmailField(max_length=254, widget=forms.TextInput(attrs={'placeholder':'Email', 'class': 'form-input'}))
teamChoices = (('red','red'),('green','green'),('blue','blue'),('orange','orange'))
team = forms.ChoiceField(choices=teamChoices)
class Meta:
model = User
fields = ('first_name','last_name','email','team','password1','password2')
I ended up solving this issue and I wanted to post it here in case anyone's stuck like I was:
If you extended the UserCreationForm in your forms.py, you need to overload the def save(self, commit=True) method and you need to copy paste everything from the method from the source code into that overloaded method and then set your user.field value to your field. For my example, the line looks like this:
user.teams = self.cleaned_data['team']
class Users(models.Model):
username = models.CharField(max_length=255)
password = models.CharField(max_length=300)
password_token = models.CharField(max_length=300, default='0')
The password text input field is regular text input field. It's not password field.
How can I get password field in django?
I tried password = models.CharField(max_length=300, widget=forms.PasswordInput ) but got error.
One way if you have a forms.py in your app is to specify the widgets in your class Meta e.g.
from django.forms import ModelForm, PasswordInput
class UserForm(ModelForm):
class Meta:
model = Users
widgets = {
'password' : PasswordInput(),
}
Widgets are assigned in the forms, not on the model.
Please see the docs for forms documentation: https://docs.djangoproject.com/en/dev/ref/forms/widgets/
You can use this approach. Create a file named forms.py inside your app.
for example:
from django import forms
from models import Users
class UsersForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = Users
I extended my User Model as described in this SO Posting:
Extending the User model with custom fields in Django
However, I'm trying to create a User Create form but I get the following:
'Members' object has no attribute 'set_password'
Here is my model form:
class Members(models.Model):
user = models.OneToOneField(User)
GENDER_CHOICES = ( ... )
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
date_of_birth = models.DateField()
class Meta:
db_table='members'
def create_user_profile(sender, instance, created, **kwargs):
if created:
Members.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
....and my form....
class SignUpForm(UserCreationForm):
GENDER_CHOICES = ( ... )
email = forms.EmailField(label='Email address', max_length=75)
first_name = forms.CharField(label='First Name')
last_name = forms.CharField(label='Last Name')
gender = forms.ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES)
date_of_birth = forms.DateField(initial=datetime.date.today)
class Meta:
model = Members
fields = ('username', 'email','first_name', 'last_name')
I'm new at Django,so thanks in advance
The method you chose to extend your User model is by creating a UserProfile (which you've called Member). A Member is not a subclass of User, so you can't call User methods (like set_password) on it.
Instead, your SignUpForm's Meta model should still be User, and to get the extended UserProfile, you should call user.get_profile(). For instance, to get a user's gender, you would call user.get_profile().gender.
Read https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users for more information about extending the user profile.