I have pre-filled forms for my user (the django user + my profile one with more informations) that just doesn't want to be saved. No error message, no redirection, I press the button "edit", and then... same page, with the modifications already written. But the database isn't changed.
So, here is the code :
forms.py :
class UpdateUser(forms.ModelForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ('email', 'username')
def clean_email(self):
email = self.cleaned_data.get('email')
username = self.cleaned_data.get('username')
if email and User.objects.filter(email=email).exclude(username=username).count():
raise forms.ValidationError('Cette adresse email est déjà utilisée, veuillez en indiquer une autre')
return email
class UpdateProfil(forms.ModelForm):
class Meta:
model = Profil
fields = ("society", "thingA", "thingB", "thingC", "thingD", "thingE")
views.py :
#login_required
def edit_profile(request):
user = User.objects.get(username = request.user)
profile = Profil.objects.get(user = user)
if request.method == 'POST':
form1 = UpdateUser(request.POST, instance=user)
form2 = UpdateProfil(request.POST, instance=profile)
if form1.is_valid() and form2.is_valid():
form1.save()
form2.save()
return HttpResponseRedirect('Register/view_profile.html')
else:
form1 = UpdateUser(initial={"email": user.email,
"username": user.username})
form2 = UpdateProfil(initial={"society": profile.society,
"thingA": profile.thingA,
"thingB": profile.thingB,
"thingC": profile.thingC,
"thingD": profile.thingD,
"thingE": profile.thingE})
return render(request, 'Register/edit_profile.html', locals())
and the template:
<form action="" method="post">
{% csrf_token %}
<p>Email : </p>{{ form1.email }}
<p>Nom d'utilisateur : </p>{{ form1.username }}
<p>Société (entreprise, association, établissement...") :</p>
{{ form2.society }}
<p>Si vous êtes membres d'un ou plusieurs centres pilotes, veuillez les cocher ci-dessous :</p>
<p>thingA : {{form2.thingA }}</p>
<p>thingB : {{form2.thingB }}</p>
<p>thingC : {{form2.thingC }}</p>
<p>thingD : {{form2.thingD }}</p>
<p>thingE : {{form2.thingE }}</p>
<button type="submit">Editer</button>
</form>
models.py
class Profil(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
deadline = models.DateField(blank=True, null=True,
default=None,
verbose_name="Date limite de validité")
thingA = models.BooleanField(default=False)
thingB = models.BooleanField(default=False)
thingC = models.BooleanField(default=False)
thingD = models.BooleanField(default=False)
thingE = models.BooleanField(default=False)
society = models.CharField(max_length = 255,
blank=True, null=True,
verbose_name="Société",
help_text="Le nom de votre entreprise, association, établissement...")
def __str__(self):
return "Profil de {0}".format(self.user.username)
After trying different thing and reading everything I could on the topic without finding a solution working, I try to think outside the box : I thought that maybe it was because I didn't put the password inside the form, but it should'nt play a role here, right ?
So, maybe a permissions problem ? I try giving permisions, for both the model. Still not working...
Thanks for your time, and sorry for my English mistakes...
So, I tinkered with my code a bit, and it's now working, I don't know why, and if someone know, I'm all ears!
I'll put it here, in case it could be helpful for others.
so, new view's code :
#login_required
def edit_profile(request):
user = User.objects.get(username = request.user)
profile = Profil.objects.get(user = user)
if request.method == 'POST':
form1 = UpdateUser(request.POST, instance=user)
form2 = UpdateProfil(request.POST, instance=profile)
if form1.is_valid() and form2.is_valid():
form1.save()
form2.save()
return redirect("viewProfile") # Here is the only change I finished with
else:
form1 = UpdateUser(initial={"email": user.email,
"username": user.username})
form2 = UpdateProfil(initial={"society": profile.society,
"thingA": profile.thingA,
"thingB": profile.thingB,
"thingC": profile.thingC,
"thingD": profile.thingD,
"thingE": profile.thingE})
return render(request, 'Register/edit_profile.html', locals())
So, I guess the problem was with the httpResponseRedirect, don't know why, since there was no error, and nothing was saved, but well... I won't look a gift horse in the mouth! It's working now. 2020 suddenly seems a lot better! Until tomorrow that is =P
Thanks to those who tried to help me!
Related
I need to create a form of persons, where the camp Name must receive an API response. I created the formulary e rendered the api response in template, but I can´t put it in my formulary, in order to save in my Models camp Name. So, I just want to save my API respone inside my forms and in my database.
Views
def cadastro(request):
url = 'https://gerador-nomes.herokuapp.com/nome/aleatorio'
api = requests.get(url)
nome_api = ' '.join(api.json())
form = PessoaForm()
form.nome = api
if request.method == 'POST':
form = PessoaForm(request.POST)
if form.is_valid():
form.cleaned_data('nome')
form.save()
return redirect('/')
context = {'form': form, 'api': nome_api}
return render(request, 'base/pessoa_form.html', context)
pessoa_form.html
<body>
<form action="" method="post">
{% csrf_token %}
{{form}}
<input type="submit" name="Cadastrar">
</form>
</body>
</html>
Forms
from django.forms import ModelForm
from . models import Pessoa
class PessoaForm(ModelForm):
class Meta:
model = Pessoa
fields = '__all__'
Models
from django.db import models
class Pessoa(models.Model):
name= models.CharField(max_length=255, null=True)
lastname= models.CharField(max_length=255, null=True)
age= models.IntegerField(null=True)
birthday_date= models.DateField()
email = models.CharField(max_length=255, null=True)
nickname= models.CharField(max_length=255, null=True, blank=True)
note = models.CharField(max_length=500, null=True, blank=True)
def __str__(self):
return self.nome
class Meta:
ordering = ['nome', 'sobrenome']
I have tried some things of my head but nothing actually worked, like try to access the variable Name in my forms inside my template and inside my views.
[English]
Well, by judging by the variable names you've chosen, I guess you're brazilian, so I'll post the answer in portuguese as well.
I don't know if this is the best practice, but you could just append the api response as a new field in the request.POST, for instance:
[Portuguese Translation]
Pelos nomes que tu usou, imagino que sejas BR então vou botar a resposta em Português também além do inglês.
Não sei se é o método que segue as melhores práticas, mas você poderia simplesmente adicionar a resposta da API como um campo novo no teu request.POST, por exemplo:
def cadastro(request):
url = 'https://gerador-nomes.herokuapp.com/nome/aleatorio'
api = requests.get(url)
nome_api = ' '.join(api.json())
form = PessoaForm()
if request.method == 'POST':
updated_request = request.POST.copy()
updated_request.update({'name': nome_api})
form = PessoaForm(updated_request)
if form.is_valid():
form.save()
return redirect('/')
context = {'form': form, 'api': nome_api}
return render(request, 'base/pessoa_form.html', context)
I have simple cv upload class for users to upload their resume. it works just fine but when they upload a newer one, the previous wont get deleted.
this is my code:
class ResumeDocument(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
cvfile = models.FileField(upload_to="documents", null=True, validators= [validate_file_extension])
#property
def filename(self):
return os.path.basename(self.cvfile.name)
how can I reach the previous id? id = self.id - 1. something like that.
this is my views:
#login_required
def pdf_resume(request):
if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect('pdf_resume')
if 'delete' in request.GET:
return delete_item(ResumeDocument, request.GET['id'])
else:
form = DocumentForm()
documents = ResumeDocument.objects.filter(user=request.user)
if documents:
form = DocumentForm(instance=documents[0])
context = {
'form':form,
'documents':documents,
}
return render(request, 'reg/pdf_resume.html', context)
and this is also my HTML code:
<form id="document-form" method="POST" enctype="multipart/form-data" action="{% url 'pdf_resume' %}" autocomplete="off" class="ant-form ant-form-horizontal">
{% csrf_token %}
<p>{{ form }}</p>
<div class="ant-row-flex" style="margin-left: -10px; margin-right: -10px;"></div>
<button id="btn_submit" type="submit"
class="ant-btn ant-btn-primary"
ant-click-animating-without-extra-node="false" style="float: left;"><span>upload</span></button>
</form>
One possible solution is to save all uploaded CVs for each user and track the last uploaded CV with field uploaded_on. You may refer to the below high-level example:
class CVFile(models.Model):
file_name = models.CharField(max_length=200)
cv_file = models.FileField(upload_to="documents", null=True, validators=[validate_file_extension])
uploaded_on = models.DateTimeField(default=timezone.now, blank=True)
class ResumeDocument(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
cv_file = models.ForeignKey(CVFile, on_delete=models.CASCADE)
You will need to amend your HMTL and views.py accordingly.
models.py
class ResumeDocument(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
cvfile = models.FileField(upload_to="documents", null=True, validators=
[validate_file_extension])
views.py(i just look is there any resumedocuments for that user if yes so i give him
the form just if he was editing the existing file)
#login_required
def pdf_resume(request):
if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect('pdf_resume')
if 'delete' in request.GET:
return delete_item(ResumeDocument, request.GET['id'])
else:
form = DocumentForm()
documents = ResumeDocument.objects.filter(user=request.user)
if documents:#new
form = DocumentForm(instance=documents[0])
context = {
'form':form,
'documents':documents,
}
return render(request, 'reg/pdf_resume.html', context)
now inside your pdf_resume.html your can just pass the 'form' because you already check if that user has a document or not.
how do I get only the text field data from a django modelform for email submission? Ive tried a lot of variations in the view, cleaned_data.get() def clean(self): methods etc. Not sure why this is so hard.
# models.py - service model
class Service(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
service_type = models.ManyToManyField('ServiceType')
description = models.TextField(blank=True)
def __str__(self):
return "Service Order for: {user}".format(user=self.user)
class ServiceType(models.Model):
label = models.CharField(max_length=200, unique=True)
def __str__(self):
return self.label
# forms.py
class NewServiceForm(forms.ModelForm):
accept_tos = forms.BooleanField(required=True)
class Meta:
model = Service
fields = '__all__'
exclude = ('user', 'created_at',)
# views.py
#login_required
def newservice(request):
if request.method == 'POST':
form = NewServiceForm(request.POST)
if form.is_valid():
form.instance.user = request.user
service = form.save()
# SEND EMAIL - send copy of project/service modelform data to user and site owner
name = request.user.first_name+request.user.last_name
username = request.user.username
subject = 'New Service Order'
from_email = request.user.email
service_data = form.cleaned_data
message = ''' From: {} {} {}
\n {}
\n {}
'''.format(name, from_email, username, subject, service_data)
recipients = ['mytestemail#gmail.com', request.user.email]
try:
send_mail(subject, message, from_email, recipients)
except BadHeaderError:
return HttpResponse('Invalid header found.')
# END EMAIL
messages.warning(request, 'Success! Your service order has been submitted.')
return redirect('projects')
else:
messages.error(request, 'Invalid submission, check and try again.')
else:
form = NewServiceForm()
return render(request, 'dashboard/newservice.html', {'form': form})
# newservice.html
<form method="post" id="newprojectform">
{% csrf_token %}
Service Type <br> {{ form.service_type }} <br><br>
{{ form.accept_tos }} <br><br>
Description {{ form.description }} <br><br>
<button class="btn btn-sm" type="submit">Submit</button>
</div>
</form>
Im getting wrong output sent to my email inbox below: I just want text output of choices, and description sent to my email inbox (and not boolean=True), ie 'Edit Content Add Content Test Description goes here'
{'service_type': <QuerySet [<ServiceType: Edit Content>, <ServiceType: Add Content>, 'accept_tos': True, 'description': 'Test Description goes here'}
after lots of issues with m2m form field data retrieval I pulled data from the created model using project = form.save() object. much strife, but somewhat easier solution.
Problem description: UserProfile form doesn't save any data.
I am creating a new User and automatically create a UserProfile object for him (so I'm extending UserProfile), so I can go to admin page and fill all the fields . But when I'm trying to do it from client side, my form just doesn't catch the data.
Also the strangest moment is that I can change username and email using UserChangeForm, so I'm trying to do the same for UserProfileObject.
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
image = models.ImageField(upload_to='profile_image', blank=True)
title = models.CharField(max_length=100, default = '')
first_name = models.CharField(max_length=200, default = '')
last_name = models.CharField(max_length=200, default = '')
subject = models.ManyToManyField('Subject', related_name='tutor_type', default = '', help_text="Select a subject")
AREA_STATUS = (
('Jerusalem', 'Jerusalem'),
('Tel Aviv', 'Tel Aviv'),
('Haifa', 'Haifa'),
('Eilat', 'Eilat')
)
area = models.CharField(max_length=200, choices=AREA_STATUS, blank=True, default='', help_text='Tutor area')
# Foreign Key used because tutor can only have one area, but area can have multiple tutors
# Author as a string rather than object because it hasn't been declared yet in file.
description = models.TextField(max_length=4000, help_text="Enter a brief description about yourself")
charge = models.IntegerField(default = '0')
# ManyToManyField used because Subject can contain many tutors. Tutors can cover many subjects.
# Subject declared as an object because it has already been defined.
LANGUAGE_CHOICES = (
('English','English'),
('Hebrew','Hebrew'),
('Russian','Russian'),
('French','French'),
('Arabic','Arabic'),
)
language = models.CharField('Language', choices = LANGUAGE_CHOICES, max_length=50, null=True)
def __str__(self):
return self.user.username
def display_subject(self):
"""
Creates a string for the subject. This is required to display subject in Admin.
"""
return ', '.join([ subject.name for subject in self.subject.all()[:3] ])
display_subject.short_description = 'Subject'
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender = User)
forms.py::
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = (
'username',
'email',
'password'
)
class EditExtendedProfileForm(UserChangeForm):
class Meta:
model = UserProfile
fields = '__all__'
exclude = ('user',)
views.py:
def edit_profile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect(reverse('accounts:view_profile'))
else:
form = EditProfileForm(instance=request.user)
args = {'form': form}
return render(request, 'accounts/edit_profile.html', args)
def edit_extended_profile(request):
if request.method == "POST":
form = EditExtendedProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect(reverse('accounts:view_profile'))
else:
return redirect(reverse('accounts:edit_extended_profile'))
else:
form = EditExtendedProfileForm(instance = request.user)
args = {'form':form}
return render(request, 'accounts/edit_extended_profile.html', args)
edit_extended_profile.html:
{% extends "base.html" %}
{% block head %}
<title>Profile</title>
{% endblock %}
{% block body %}
<div class = "container">
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button type = "submit" class = "btn btn-success">Submit</button>
</form>
</div>
{% endblock %}
and it is the same template as for edit_profile view.
No traceback, no errors. Any help will be appreciated. Thanks in advance.
I had to create a form from which some details go to default.auth.user model and some to my custom model so after searching from various sources I did this:
Django Version :1.7
model.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
title_id = models.ForeignKey('Title')
mobile_number = models.CharField(max_length=10)
alternate_number = models.CharField(max_length=10)
date_of_birth = models.DateField()
profession_id = models.ForeignKey('Profession', null=True, blank=True)
house_no = models.CharField(max_length=100, blank=True, default='NA')
city_id = models.ForeignKey('City', null=True)
country_id = models.ForeignKey('Country', null=True)
state_id = models.ForeignKey('State', null=True)
locality_id = models.ForeignKey('Locality', null=True)
profile_picture_path = models.CharField(max_length=100, blank=True, default='NA')
forms.py:
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={'id': 'password'}))
email = forms.CharField(widget=forms.TextInput(attrs={'id': 'email_id'}))
username = forms.CharField(widget=forms.TextInput(attrs={'id': 'username'}))
first_name = forms.CharField(widget=forms.TextInput(attrs={'id': 'first_name'}))
last_name = forms.CharField(widget=forms.TextInput(attrs={'id': 'last_name'}))
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name', 'password')
class ExtraDetailsForm(UserForm):
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs=
{'id':'confirm_password'}),max_length=32,
required=True,)
class Meta:
model = UserProfile
fields = ('email', 'username', 'title_id', 'first_name', 'last_name',
'password', 'confirm_password',
'date_of_birth', 'mobile_number', )
My view.py is :
def register(request):
# A boolean vakue for telling whether the registration was successful
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
additional_details_form = ExtraDetailsForm(data=request.POST)
if user_form.is_valid() and additional_details_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
additional_details = additional_details_form.save(commit=False)
additional_details.user = user
additional_details.save()
registered = True
else:
print(user_form.errors, additional_details_form.errors)
else:
user_form = UserForm
additional_details_form = ExtraDetailsForm
return render(request,
'users/register.html',
{'user_form' : user_form, 'additional_details_form': additional_details_form, 'registerered': registered})
regsiter.html:
{% if registerered %}
<p>Thank you for register. check ur email , entered email was</p>
{% else %}
<form action="/users/register/" method="post">{% csrf_token %}
{{ additional_details_form.as_p }}
<input type="submit" value="Register" />
</form>
{% endif %}
Now the good thing is that everything is working fine and details are being stored as they should be.
But the bad thing is I do not know whether it is a correct approach or not as I did not find any tutorial/resource where this approach is used?
This is correct approach and you do it almost right. Just couple notes:
if user_form.is_valid() and additional_details_form.is_valid():
In this line if user_form is invalid then validation for additional_details_form will not run. To always validate both change it to:
if all([user_form.is_valid(), additional_details_form.is_valid()]):
In else statement you set form class to *_form variables. It should be form instances instead:
user_form = UserForm()
additional_details_form = ExtraDetailsForm()
And it may be a good idea to wrap your save code into one transaction :-)
I would recommend that you use just one form here that contains all fields.
There is no benefit to using two forms, especially since one inherits the other, this is odd behaviour when you are then passing the POST data into each of them.
Consolidate the fields into a single form and then override the 'clean' method of the form to be able to check that the two password fields match.
You can create a single form to save data into one or many different models and this is especially useful in your case since you need to validate the data for these different models together.
Ok, firstly ExtraDetailsForm shouldn't inherit from UserForm because they are for different models. It should look something like this instead:
class UserForm(forms.ModelForm):
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs=
{'id':'confirm_password'}),max_length=32,
required=True,)
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name', 'password',
'confirm_password')
class ExtraDetailsForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('title_id', 'date_of_birth', 'mobile_number')
Then in your view:
from django.contrib.auth import login
from django.shortcuts import redirect, render
def register(request):
user_form = UserForm(data=request.POST or None)
profile_form = ExtraDetailsForm(data=request.POST or None)
if all([user_form.is_valid(), profile_form.is_valid()]):
user = user_form.save(commit=False)
user.set_password(user.password)
user.save()
profile = profile_form.save()
# probably at this point you want to login the new user:
login(request, user)
# it's good practice to do a redirect here, after a successful
# form post, eg to display success page, as this will
# prevent accidental re-posting data if user reloads the page
return redirect('registration_success')
else:
print(user_form.errors, profile_form.errors)
return render(
request,
'users/register.html',
{
'user_form' : user_form,
'profile_form' : profile_form,
}
)
def registration_success(request):
return render('registration_success.html')
Finally you need to output both forms in the template:
<form action="/users/register/" method="post">{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" value="Register" />
</form>
and a new template registration_success.html:
<p>Thank you for registering. Check your email, entered email was: {{ request.user.email }}</p>