Edit Profile gives me algorithm, iterations and hash in Django - python

When I go to my "edit profile" page this is what it looks like:
So it shows me the algorithm, iterations and hash. And if I want to remove it form my forms.py (remove password):
class EditProfileForm(UserChangeForm):
template_name='/something/else'
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name',
**'password'**
)
It shows me this:
Feel free to share your ideas.

I changed my code form forms.py to an editable code in edit_profile.html (that takes the entries form forms.py):
edit_profile.html:
<form method="post">
{% csrf_token %}
<label for="first_name">Vorname </label>
<input style="margin-bottom: 1em;" id="first_name" class="form-control" type="text" maxlength="25" name="first_name" value="{{ user.first_name }}"><br>
<label for="last_name">Nachname </label>
<input style=" margin-bottom: 1em;" id="last_name" class="form-control" type="text" maxlength="25" name="last_name" value="{{ user.last_name }}"><br>
<label for="email">E-Mail </label>
<input style="margin-bottom: 1em;" id="email" class="form-control" type="email" maxlength="50" required=True unique=True name="email" value="{{ user.email }}"><br>
<button class="btn btn-success btn-sm" type="submit">Bestätigen</button>
<a style="margin-left: 1em;" href="{% url 'accounts:view_profile' %}" id="cancel" name="cancel" class="btn btn-default">Zurück</button></a>
{% if form.errors %}
<p style="margin-left: 5em;" class=" label label-danger">
Das ist keine gültige E-Mail-Adresse. Bitte versuche Sie es erneut.
</p>
{% endif %}
</form>

Related

I want to enable or disable fields for some users from admin panel

This is template
<div class="container mt-3">
<h5 class="card-title">Kindly enter the details. All details are required</h5>
<form class="row g-3" method="post" action="{% url 'home' %}" enctype="multipart/form-data">
{% csrf_token %}
<div class="col-md-4">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-4">
<label for="parentage" class="form-label">Father's Name</label>
<input type="text" class="form-control" id="parentage" name="father" required >
</div>
<div class="col-md-4">
<label for="mothername" class="form-label">Mother's Name</label>
<input type="text" class="form-control" id="mothername" name="mother" required>
</div>
<div class="col-md-4">
<label for="phonenumber" class="form-label">Phone number</label>
<input type="tel" class="form-control" id="phonenumber" name="pnumber" required>
</div>
<div class="col-md-4">
<label for="ephonenumber" class="form-label">Emergency contact number</label>
<input type="tel" class="form-control" id="ephonenumber" name="enumber" required>
</div>
<div class="col-md-4">
<label for="dob" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="dob" name="dob" required>
</div>
<div class="col-12">
<label for="inputAddress" class="form-label">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="" name="address" required>
</div>
<div class="col-md-6">
<label for="photo" class="form-label">Upload a photo</label>
<input type="file" class="form-control" id="photo" name="photo" accept="image/*,.pdf" required>
</div>
<div class="col-md-4">
<label for="schoolname" class="form-label">School Name</label>
<input type="text" class="form-control" id="schoolname" name="sname" required >
</div>
<div class="col-md-4">
<label for="inputState" class="form-label">Role</label>
<select id="select" class="form-select" name="role" required>
<option selected>Choose...</option>
<option value="student">Student</option>
<option value="teacher">Teacher</option>
</select>
</div>
<div id="show">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
This is models.py
class Student(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,null=True)
fullname= models.CharField(max_length=50)
father = models.CharField(max_length=30)
mother = models.CharField(max_length=30)
phno=models.IntegerField()
ephno = models.IntegerField()
dob=models.DateField()
address=models.CharField(max_length=100)
photo= models.ImageField(upload_to='photos/')
role=models.CharField(max_length=20)
rollNumber = models.IntegerField()
standard = models.CharField( max_length=10)
def __str__(self):
return self.fullname
This is admin.py
class studentAdmin(ExportActionMixin,admin.ModelAdmin):
list_display=('fullname', 'father',"mother",'phno','ephno',"dob",'address','photo','role','rollNumber','standard')
admin.site.register(Student,studentAdmin)
class teacherAdmin(ExportActionMixin,admin.ModelAdmin):
list_display=('fullname', 'father',"mother",'phno','ephno',"dob",'address','photo','role')
list_filter =['user']
admin.site.register(Teacher,teacherAdmin)
I want admin to enable or disable form fields and model fields from the admin panel.
Is there any way to do the same? For some users, I want to for example keep the mother's name and for some, I want to hide to it

I am working on a website that requires users to register and login but Having problem in adding an additional field to auth_user using django

I have this form in html of my site
<form class="row contact_form" action="." method="post" novalidate="novalidate">
{% csrf_token %}
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="firstname" name="first_name" value=""
placeholder="First Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="lastname" name="last_name" value=""
placeholder="Last Name" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="mobile" name="mobile" value=""
placeholder="Mobile Number" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="text" class="form-control" id="email" name="email" value=""
placeholder="Email" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="password" name="password" value=""
placeholder="Password" required>
</div>
<div class="col-md-12 form-group p_star">
<input type="password" class="form-control" id="cpassword" name="cpassword" value=""
placeholder="Confirm Password" required>
</div>
<div class="col-md-12 form-group">
<button type="submit" value="submit" class="btn_3">
SignUp
</button>
</div>
</form>
Now the problem I am facing is:
1)When I try to post data it shows me an error that username is required which i dont want(create_user() missing 1 required positional argument: 'username').
2)There is no mobile number section in auth_user which i want.
I have tried reading the django docs but couldnt understand(OnetoOne thing and custom usermodel thing I couldnt understand both)
Here is my views.py
def signup(request):
if request.method == "POST":
first_name=request.POST['first_name']
last_name=request.POST['last_name']
email=request.POST['email']
mobile=request.POST['mobile']
password=request.POST['password']
cpassword=request.POST['cpassword']
username=request.POST['username']
user=User.objects.create_user(first_name=first_name,last_name=last_name,email=email,password=password,mobile=mobile)
user.save();
return redirect('/')
else:
return render(request,"signup.html")
(The indentations are correct)
Also I want that the users can login using mobile number or email but I dint find any explanation for that.
from django.contrib.auth.models import User
...
class MyUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# add other custom fields
first_name = models.CharField(...)
...
# create as
user = User.objects.create_user(username=email,...)
my_user = MyUser.objects.create(user=user, first_name=first_name, ...)
# authenticate as
user.authenticate(username=email, password=password)
This might help
Django default auth

Image not being uploaded .I want to upload the image and save it into the database i've given the media root and media url

applyonline.html(I have given file fields for adhaarcopy and idcopy)
<body ng-app="">
{% extends "pmmvyapp/base.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% block content%}
<div class="col-md-8">
<form method="post" action="/personal_detail/">
{% csrf_token %}
<div class="form-group">
<div class=" mb-4">
<h6><u>(*Mandatory Fields)Please Fill up the details below </u></h6>
</div>
<legend class="border-bottom mb-4" ,align="center">1.Beneficiary Details</legend>
<label for="formGropuNameInput">Does Beneficiary have an Adhaar Card?*</label>
<input type="radio" name="showHideExample" ng-model="showHideTest" value="true">Yes
<input type="radio" name="showHideExample" ng-model="showHideTest" value="false">No
<!--logic for yes-->
<div ng-if="showHideTest=='true'">
<div class="form-group">
<label for="formGropuNameInput">Name of Beneficiary(as in Aadhar Card)*</label>
<input name="beneficiary_adhaar_name" class="form-control" id="formGroupNameInput" placeholder="Enter name of Beneficiary as in Aadhar Card" required>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile" name="adhaaarcopy">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
<!--logic for no-->
<div ng-if="showHideTest=='false'">
<div class="form-group">
<div class="form-group">
<label for="adhaar_eid">Aadhaar Enrollment ID(EID):</label>
<input name="adhaar_eid" id="identityno" class="form-control" required>
</div>
<div class="form-group">
<label for="formGropuNameInput">Name of Beneficiary(as in Identity Card)* </label>
<input name="beneficiary_id_name" class="form-control" id="formGroupNameInput" placeholder="Enter your name" required>
</div>
<div class="form-group">
<label for="idno">Identity Number(Enclose Copy of Identity Card)*:</label>
<input name="idno" id="identityno" class="form-control" required>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="idcopy" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" style="margin-bottom:10px ">Submit</button>
</form>
</div>
{% endblock %}
</body>
this is my views.py i've used POST.get
#login_required
def personal_detail(request):
# ShowHideExample = request.POST.get('showHideExample',False)
beneficiary_adhaar_name=request.POST.get('beneficiary_adhaar_name')
adhaarno=request.POST.get('adhaarno')
adhaarcopy=request.POST.get('adhaarcopy')
idcard=request.POST.get('idcard')
adhaar_eid=request.POST.get('adhaar_eid')
beneficiary_id_name=request.POST.get('beneficiary_id_name')
idno=request.POST.get('idno')
idcopy=request.POST.get('idcopy')
apply_online = Personal_Detail(beneficiary_adhaar_name=beneficiary_adhaar_name,adhaarno=adhaarno,adhaarcopy=adhaarcopy,
idcard=idcard,adhaar_eid=adhaar_eid,beneficiary_id_name=beneficiary_id_name,idno=idno,idcopy=idcopy)
apply_online.save()
return render(request,'users/applyonline.html')
this is my models.py where i have created FileField for both
class Personal_Detail(models.Model):
beneficiary_adhaar_name=models.TextField(blank=True, null=True)
adhaarno=models.IntegerField(blank=True, null=True)
adhaarcopy = models.FileField(upload_to='adhaar/')
idcard=models.TextField(blank=True, null=True)
adhaar_eid=models.IntegerField(blank=True,null=True)
beneficiary_id_name=models.TextField(blank=True, null=True)
idno=models.IntegerField(blank=True, null=True)
idcopy=models.FileField(upload_to='identitycard/')
def __str__(self):
return self.beneficiary_adhaar_name or self.beneficiary_id_name
settings.py I have given the media root and provided the media url
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL ='/media/'
CRISPY_TEMPLATE_PACK='bootstrap4'
Like I want to know how can I upload the images like what should be my next steps.
You should get the file after submitted the form as below...
idcopy = request.FILES['idcopy']
adhaaarcopy = request.FILES['adhaaarcopy']
insted of...
idcopy=request.POST.get('idcopy')
adhaaarcopy=request.POST.get('adhaaarcopy')
And so on for other fields...
After that you can save and get it in appropriate location in media.

POST no save to model. return "this field is required" error on Django

Hi friends I am new for Django
I want to make a form with bootstrap.
When I click submit Button Post Data is
<QueryDict: {'csrfmiddlewaretoken': ['mjzoKZ1GPuHMCR6LQwNpUxDW0Y1KyxRgySSynPSOU7eHK8Zlm5dgqvWwhXpD3F0M'], '<input type="text" name="borclu" maxlength="36" required id="id_borclu">': ['asfasf'], '<input type="email" name="borclu_mail" maxlength="25" required id="id_borclu_mail">': ['asdasd'], '<textarea name="borcluAdres" cols="40" rows="10" maxlength="80" required id="id_borcluAdres">\r\n</textarea>': ['asdasd'], '<input type="text" name="borcluTelefon" maxlength="11" required id="id_borcluTelefon">': ['asdasd'], '<input type="number" name="tutar" required id="id_tutar">': ['asdasd'], '<input type="text" name="cek_tarih" required id="id_cek_tarih">': ['12-02-2019']}>
But data no saved to model
My model like that;
class cekler(models.Model):
borclu=models.CharField(max_length=36,verbose_name="Keşideci")
borcluAdres=models.TextField(max_length=80,verbose_name="Keşideci Adresi")
borclu_mail = models.EmailField(max_length=25,verbose_name="Keşideci E Posta Adresi")
borcluTelefon=models.CharField(max_length=11,verbose_name="Keşideci Telefon")
tutar=models.IntegerField(verbose_name="Tutar")
bankalar = (
('Ziraat Bankası', 'Ziraat Bankası'),
('YapıKredi Bankası', 'YapıKredi Bankası'),
('Vakıflar Bankası', 'Vakıflar Bankası'),
('Teb', 'TEB'),
)
banka=models.CharField(max_length=20, choices=bankalar, verbose_name="Banka Adı:")
cek_tarih=models.DateField(verbose_name="Çek Tarihi")
son_tarih = models.DateTimeField(blank=True, null=True)
def _get_gecen_gun(self):
"Returns the person's full name."
day=date.today()-self.cek_tarih
ggun = str(day)
ggun = ggun.split(' ')[0]
return '%s' % (ggun)
gecenGun = property(_get_gecen_gun)
def __str__(self):
return self.borclu
def save(self, *args, **kwargs):
if not self.pk:
self.son_tarih = self.cek_tarih + datetime.timedelta(days=80)
super(cekler, self).save(*args, **kwargs)
class Meta:
ordering = ['-id']
My Form.py;
from django import forms
from .models import cekler
class CekForm(forms.ModelForm):
class Meta:
model=cekler
fields=[
'borclu',
'borcluAdres',
'borclu_mail',
'borcluTelefon',
'tutar',
'banka',
'cek_tarih',
]
My Template
<form method="post">
{% csrf_token %}
<div class="container">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Keşideci Adı</label>
<input type="text" name='{{ form.borclu }}' class="form-control" >
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Borçlu Mail</label>
<input type="text" name='{{ form.borclu_mail }}' class="form-control" >
</div>
</div>
<div class="form-group">
<label for="inputAddress">Borçlu Adres</label>
<input type="text" name='{{ form.borcluAdres }}' class="form-control" >
</div>
<div class="form-group">
<label for="inputAddress2">Borçlu Telefon</label>
<input type="text" name='{{ form.borcluTelefon }}' class="form-control" >
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputCity">Tutar</label>
<input type="text" name='{{ form.tutar }}' class="form-control" >
</div>
<div class="form-group col-md-4">
<label for="inputState">Banka</label>
<select id="inputState" class="form-control">
{% for value in form.banka %}
<option >{{ value }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-2">
<label for="inputZip">Çek Tarihi</label>
<input type="text" name='{{ form.cek_tarih }}' class="form-control" >
</div>
</div>
<input type="submit" class="btn btn-primary" value="Ekle">
</div>
</form>
I think problem from my Bootstrap implementation. Before this implemantation I use form with 'form.as_p' and it was correct.
Now when I click button anything happen. No error. But data no save on my model.
You're putting the wrong thing in your template. You should be able to see from the submitted data that each field has some HTML as its key, which is obviously wrong.
Instead of name='{{ form.borclu }}' etc you should probably do name='{{ form.borclu.name }}'.

None value passed on editing the form on HTML

I am editing the Firtname and Lastname fetched from auth_User. When I edit the name and save, the None value is passed to the names. On verifying page source I dont see any error.
form.py
from django.contrib.auth.models import User
class UserEditForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name','last_name', 'email')
view.py
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance=request.user,
data=request.POST)
if user_form.is_valid():
user_form.save()
messages.success(request, 'Profile updated successfully!')
else:
messages.success(request, 'Error updating your profile!')
else:
user_form = UserEditForm(instance=request.user)
return render(request,
'account/edit.html',
{'user_form': user_form})
HTML
<div class="form-row">
<label class="col-sm-2 control-label" placeholder="First Name">First Name:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="first_name" value="{{ user_form.first_name.value }}">
</div>
<label class="col-sm-2 control-label">Last Name:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="last_name" value="{{ user_form.last_name.value }}">
</div>
</div>
<div class="form-row">
<label for="email" class="col-sm-2 control-label">Email address:</label>{{ user_form.email }}
<div class="col-sm-4">
<input type="email" class="form-control" id="email" value="{{ user_form.email.value }}">
</div>
</div>
You do not have any name attributes on your form fields, so no data will be submitted. Add a name attribute to both fields:
<input name="first_name" type="text" class="form-control" id="first_name" value="{{ user_form.first_name.value }}" >
<!-- ^^^^ note new name attribute -->
and:
<input name="last_name" type="text" class="form-control" id="last_name" value="{{ user_form.last_name.value }}" >
Note also that you have specified email in the fields on your form, but there is no email field in your HTML, which will also cause problems. You should either add that field to your HTML or remove it from the form definition.
You might also consider using Django's form helpers to render the fields.
The <input> tags you have created in your template do not include name attributes. When you attempt to submit the form, these items will not be sent.
The correct name for each form field can be accessed using the html_name attribute, eg:
{{ user_form.first_name.html_name }}
#solarissmoke mentioned that you should consider using the Django form helpers, and I strongly agree. If you don't like how default widgets appear in your HTML, you can add classes when you create the form:
from django.contrib.auth.models import User
class UserEditForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name','last_name', 'email')
widgets = {
'first_name': forms.TextInput(attrs={'class': 'form-control'}),
'last_name': forms.TextInput(attrs={'class': 'form-control'}),
'email': forms.EmailInput(attrs={'class': 'form-control'}),
}
Then in your template:
<div class="form-row">
<label for="{{ user_form.first_name.auto_id }}" class="col-sm-2 control-label" placeholder="First Name">First Name:</label>
<div class="col-sm-4">
{{ user_form.first_name }}
</div>
<label for="{{ user_form.last_name.auto_id }}" class="col-sm-2 control-label">Last Name:</label>
<div class="col-sm-4">
{{ user_form.last_name }}
</div>
</div>
<div class="form-row">
<label for="{{ user_form.email.auto_id }}" class="col-sm-2 control-label">Email address:</label>
<div class="col-sm-4">
{{ user_form.email }}
</div>
</div>

Categories