Django .cleaned_data.get() - django

class UserRegistration:
"""This class doesn't inherit to any parent class."""
def register(request):
if request.method == 'POST':
"""Then takes the POST data."""
form = UserCreationForm(request.POST)
# Check the data from the form if it's valid
if form.is_valid():
username = form.cleaned_data.get('username')
where's that username coming from that has been passed as a parameter in the cleaned_data?

Related

Form Django can't update data

here's my code
i can't update my data if i used type "file"
My form can't be updated, I try print(form.error) but "this field requirement" appears, even though the form is filled out
views.py :
#login_required
def data_karyawan_edit(request, id):
karyawan = Karyawan.objects.get(id=id)
ar_divisi = Divisi.objects.all()
if request.method == 'POST':
form = KaryawanForm(request.POST, request.FILES, instance=karyawan)
print(form.errors)
if form.is_valid():
form.save()
messages.success(request, "Berhasil disimpan")
return redirect('data_karyawan')
else:
form = KaryawanForm()
return render(request, "data_karyawan/edit.html", {'karyawan': karyawan, 'ar_divisi': ar_divisi})

Change username into new user name, Django

I want the user to be able to change his username
so i tried this code and its not working any one can help!
def edit(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
new_name = request.POST['new_name']
user = authenticate(username=username,password=password)
if user is not None:
user = authenticate(username=new_name,password=password)
return redirect('userr')
return render(request, 'index/edit.html')
`#--------- forms.py ---- Need to import in view.py this form
class UserProfileForm(forms.ModelForm):
class Meta:
# indicate User model to form
model = User
# enter name of fields which model have
fields = ['username']
#------------ View.py
from .form import UserProfileForm
def edit(request):
# -- fetch value from model
user = get_object_or_404(UserProfileForm, user = request.user)
# check request
if request.method == 'POST':
# grab user value using Django forms
user_form = UserProfileForm(request.POST)
# using is_valid() method check form is valid or not
if user_form.is_valid():
# Save user
user_form.save()
messages.success(request,'Username Updated.')
return redirect('user')
return render(request, 'index/edit.html')
# it will save new username in database
`
Note: Try code as per your variable names

Why my form in Django is not valid ? Never do the condition

I'm trying to save the default data of my form and model with a simple function in Django but when I use the condition of is_valid() never enter to the condition. What can I do ?
This is for views.py
def entreno1B(request):
if request.method == 'POST':
form=Entreno(request.POST)
if form.is_valid():
print("is valid")
form.save()
return render(request, 'training/entrenosB1.html')
my model is
class Entrenos(models.Model):
tiempo1D1 = models.FloatField(max_length=1000,default=0.0)
tiempo2D1 = models.FloatField(max_lenght=1000,default=0.0)
and my form is
class Entreno(forms.ModelForm):
class Meta:
model = Entrenos
fields = ('tiempo1D1','tiempo2D1',)
Please Help. I don't know what is my error. Thank you
Also, when I modify my views like this:
def entreno1B(request):
if request.method == 'POST':
form=Entreno(request.POST)
form.save()
if form.is_valid():
print("is valid")
form.save()
return render(request, 'training/entrenosB1.html')
I got the error that my object has no attibute 'cleaned_data'
I modify my function in views.py, and works. I do this:
def entreno1B(request):
if request.method == 'POST':
form=Entreno({'tiempo1D1':DATA, 'tiempo2D1':Data2})
if form.is_valid():
print("is valid")
form.save()
return render(request, 'training/entrenosB1.html')

How to let the user to edit the custom user model details in the user dashboard and update it in the model in django?

I am having edit profile template and I wrote view for letting the user to edit the account but I am not getting the form even I wrote the url and view correctly can you please help me out how to let the user edit user model in the front end
my views.py:
def edit_profile(request):
if request.method == 'POST':
form = UserChangeForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('modsy:account'))
Forms.py:
class EditProfileForm(UserChangeForm):
template_name='edit_profile'
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name',
'password'
)
else:
form = UserChangeForm(instance=request.user)
args = {'form': form}
return render(request,'edit_profile.html')
I am only getting the submit button in editprofile page but form is not coming can you please say what mistake I had did
You are handling only for POST request only but not for GET.Also here your form name is EditProfileForm not UserChangeForm so change your view like this:
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('modsy:account'))
else:
form = EditProfileForm()
return render(request,'Your_template',{'form':form})

local variable 'post' referenced before assignment

Im trying to assign the current logged in username to the existing model through a form. But facing a problem while saving the form
#views.py
def create(request):
if request.method == "POST":
form=NotForm(request.POST)
if form.is_valid():
post.user = request.user.get_username()
post = form.save(commit=False)
post.save()
return redirect('base')
else:
form=NotForm()
return render(request, 'create.html',{'form':form})
forms.py
class NotForm(forms.ModelForm):
class Meta:
model=Note
fields = ('title', 'desc',)
models.py
class Note(models.Model):
title=models.CharField(max_length=50)
desc=models.TextField(max_length=200)
user=models.ForeignKey('auth.User')
if request.method == "POST":
form=NotForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user.get_username()
post.save()
return redirect('base')
try this in the view