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
Related
I have followed this tutorial to test out the User authentication and Signals in Django. I don't know what I should do with this part (found from the first post of this tutorial):
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
birthdate = forms.DateField()
discord_id = forms.CharField(max_length=100, help_text='Discord ID')
zoom_id = forms.CharField(max_length=100, help_text='Zoom ID')
text = forms.TextField(null=True, blank=True)
class Meta:
model = User
fields = ["username", "password1", "password2", "birthdate", "email", "discord_id", "zoom_id"]
With those imports I get an error NameError: name 'forms' is not defined and if I add an import from django import forms I get errors like AttributeError: module 'django.forms' has no attribute 'TextField'.
Sohuld I add all the fields from my Model into this RegisterForm -class I want to include to the registration process? What do I do to the fields that are textFields in my Model?
Instead of using forms.TextField which does not exist in Django, you need to use forms.CharField(widget=forms.Textarea).
I think you are trying to use CharField instead of TextField. Django uses CharField in its forms with default widget as TextArea.
You can find more details here
https://docs.djangoproject.com/en/3.2/ref/forms/fields/#charfield
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.
I have a doubt about authentification with my own Model. In my project I have a Pacient Model and I need that to be saved on the Users panel in the Admin site.
Basically I recover the data from a HTML form (they're generated in a , it was a request for the test)
(Do not worry about the validations)
models.py
class Pacient(models.Model):
name = models.CharField()
birthday = models.DateField()
email = models.EmailField()
password = models.CharField()
confirmPass = models.CharField()
In the forms.py file
class PacientForm(forms.ModelForm):
name = forms.CharField
birthday = forms.DateField
email = forms.EmailField
password = forms.CharField
confirmPass = forms.CharField
class Meta:
model = Pacient
fields =('name','birthday','email','password','confirmPass')
Using those fields I can save data in the table Pacient. It can be seen in the admin site but separated from the Groups and Users under the project name.
So, my question is this. How can I made the Pacient Model to be recognized as the User (in the admin site) and login using the "email" as the username?
You should look at the AbstractBaseUser model:
from django.contrib.auth.models import AbstractBaseUser
This Base model can be inherited by your Pacient model, then in settings.py file you can set:
AUTH_USER_MODEL = 'yourapp.Pacient'
This will tell django to use your model for auth purposes.
More infos here : https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#django.contrib.auth.models.CustomUser
I have a Crew Model. The table has a password field. I dont want to display password in plain text. So I have added forms where I have used widget=forms.PasswordInput(). But this doesnt display the password after saving the data. How do I display password in hidden format?
class Crew(models.Model):
crew_id = models.AutoField(primary_key=True)
crew_code = models.CharField(max_length=200, null=False, unique=True)
crew_name = models.CharField(max_length=200, null=False)
crew_password = models.CharField(max_length=200, null=False)
Forms.py
class UserForm(forms.ModelForm):
crew_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = Crew
fields = ('crew_name', 'crew_password', 'crew_code', 'crew_id')
In case you need to safe passwords ( I totally agree this is not the way for django users, but I do have some other connections with passwords saved) you should have a look at django-extensions, using python-keyczar to encrypt passwords).
To answer your question, i aslo found out the password widget does not show whether a password is filled in or empty. (i would expect dots or asterics to show there is something in the field, but should not be shown of course).
this does that exactly:
class SomeConnectionForm(forms.ModelForm):
class Meta:
widgets = {
'password': forms.PasswordInput(render_value = True),
}
the trick is the render_value=True
I have a model, configuration, in Django and wish to fill the author field with get_username
Can this be done within the model or must it be done from the form? If it must be on the form, how can I change the standard admin page to have this functionality?
At present, the model reads thus:
class Configuration(models.Model):
title = models.CharField(max_length=100,unique=True,blank=False)
author = models.CharField(max_length=50,blank=False)
created = models.DateTimeField("date created",auto_now_add=True)
modified = models.DateTimeField("date modified",auto_now=True)
description = models.CharField(max_length=512)
drawing = models.ForeignKey(Drawing)
instruments = models.ManyToManyField(Instrument)
def __unicode__(self):
return self.title
Use models.ForeignKey:
#models.py
from django.contrib.auth.models import User
class Configuration(models.Model):
author = models.ForeignKey(User)
...
#admin.py:
class Configuration_admin(admin.ModelAdmin):
fields = ('title', 'author',....)
something like that:
from django.contrib.auth.models import User
class ...
...
username = models.ForeignKey(User)
If you want to make some relationship between your model and default User model then you can extends the User model into your own custom model , like this:
models.py
from django.contrib.auth.models import User
class Configuration(models.Model):
author = models.OneToOneField(User)
..
..