I am trying to build a Create Profile Form after registration and my codes are below.Its already working only for a slight problem,that is,if the name field of Profile model matches one in database,django brings an error message column slug is not unique .I see that slug field is clashing with another,how am I going to edit the below codes to raise a validation error message if name submitted is similiar to one in db?
models.py
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
name= models.CharField(max_length=10)
description=models.TextField()
slug=models.SlugField(unique=True)
user = models.OneToOneField(User, blank=True, null=True)
def __str__(self):
return self.name
views.py
def create_profile(request):
form_class = ProfileForm
if request.method == 'POST':
form = form_class(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.slug = slugify(profile.name)
profile.save()
slug = slugify(profile.name)
return redirect('profile_details', slug=profile.slug)
else:
form = form_class()
return render(request, 'profile/create_profile.html', {'form': form,})
urls.py
urlpatterns=[url(r'^accounts/create_profile/$',views.create_profile,name='registration_create_profile'),]
Thanks
ModelForms actually validate uniqueness as part of the is_valid() checks.
The problem in your case is that you are manually setting the slug after running is_valid(). Because of this you will have to manually validate uniqueness after setting the slug. Something like this would work:
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.slug = slug = slugify(profile.name)
# Check slug uniqueness, keep appending a digit until it fits
n = 1
while Profile.objects.filter(slug=profile.slug).exists():
# Need to find a new slug.
profile.slug = '%s-%d' % (slug, n)
n += 1
profile.save()
return redirect('profile_details', slug=profile.slug)
Note that it is probably better to perform this uniqueness check inside the model's save() method instead of in the view - this just illustrates the logic.
Alternatively you could use a library like django-autoslug which performs this logic for you.
Related
I have a form in my django website where the user requests coins and the information is sent to the admin for me to process. I want to automatically get the user who filled the form without them doing it themselves.
Here's the model.py file:
class Requestpayment (models.Model):
username= models.ForeignKey(User, on_delete= models.CASCADE, null=True)
useremail= models.CharField(max_length=100)
accountmail= models.CharField(max_length=100)
accountphonenumber=models.CharField(max_length=15)
coinsrequested=models.ForeignKey(Requestamount, on_delete= models.SET_NULL, null=True)
created= models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.accountmail
the forms.py:
class Requestpaymentform (ModelForm):
class Meta:
model = Requestpayment
fields = '__all__'
and the views.py:
#login_required(login_url='login')
def redeemcoins (request):
form = Requestpaymentform
if request.method =='POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
form = form.save(commit=False)
username = request.user
form.save()
return redirect ('home')
I am pretty sure something is wrong but i don't know what it is (I'm very new at django) anyway the form always shows all the users in the website for the current user to pick who they are.
redeem coins page
I also tried excluding that part of the form but it didn't work it just shows up empty in the admin.
thank you.
You need to assign it to the instance wrapped in the form, so:
#login_required(login_url='login')
def redeemcoins(request):
form = Requestpaymentform()
if request.method == 'POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
form.instance.username = request.user
form.save()
return redirect('home')
# …
It makes more sense however to name this field user than username. In the model you can also make the username field non-editable, such that it does not appear in the form:
from django.conf import settings
class Requestpayment(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, editable=False, on_delete=models.CASCADE
)
# …
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
When you use username= models.ForeignKey(User, on_delete= models.CASCADE, null=True), Django add a field named user_id in your database which allow django to find User object for Requestpayment.
You can use user_id field to add a User object in Requestpayment.
You don't need to pass username field in your fields list if you want to get user in view.
class Requestpaymentform (ModelForm):
class Meta:
model = Requestpayment
#fields = '__all__'
fields = ['useremail',
'accountmail',
'accountphonenumber',
'coinsrequested',
'created']
Now do this to get user in your view.
#login_required(login_url='login')
def redeemcoins(request):
form = Requestpaymentform()
if request.method == 'POST':
form = Requestpaymentform(request.POST)
if form.is_valid():
requestpayment = form.save(commit=False)
requestpayment.user_id = request.user.id
requestpayment.save()
return redirect('home')
And it's great to use user instead username because it's a User object and not a simple field.
Please for my English !!!
I am working on a project in Django where I have an Education(models.Model) with a OneToOneField to User Model as shown below:
class Education(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
qualification = models.CharField(max_length=60, choices=INSTITUTE, blank=True, null=True)
institution = models.CharField(max_length=40, null=True)
reasons = models.CharField(max_length=100, null=True)
matnumber = models.CharField(max_length=255, null=True)
And a forms.ModelForm as shown below:
class AddEducationForm(forms.ModelForm):
class Meta:
model = Education
fields = ['qualification','instition', 'matnumber', 'reasons','refphone']
In my views.py file I want to save this AddEducationForm for the logged in user. Below is what I have tried but it is not saving but showing success message of save.
def AddEducation(request):
if request.method == 'POST':
form = AddEducationForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
messages.success(request, 'Education Added Successfully')
return redirect('user-bank')
else:
form = AddEducationForm()
context = {
'form':form,
}
return render(request, 'user/add_education.html', context)
The system is displaying the success message that the form has been saved for the logged in user but in reality it is not (when checked using Django Admin Login).
Someone should kindly help out with the solution to this problem. Remember each logged in user would save only one Education Form record. Thank in anticipation.
You add it to the instance of the form, so:
from django.contrib.auth.decorators import login_required
#login_required
def add_education(request):
if request.method == 'POST':
form = AddEducationForm(request.POST, request.FILES)
if form.is_valid():
form.instance.applicant = request.user
form.save()
messages.success(request, 'Education Added Successfully')
return redirect('user-bank')
else:
form = AddEducationForm()
context = {
'form':form,
}
return render(request, 'user/add_education.html', context)
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
Note: Functions are normally written in snake_case, not PascalCase, therefore it is
advisable to rename your function to add_education, not addEducation.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Whenever, I call form.save() I get "django.db.utils.IntegrityError: UNIQUE constraint failed: legal_useragreedtolegal.user_id"
I think this might be because I have a oneToOneField and Django is trying to save to UserAgreedToLegal and User Model but the User model already has that ID, so the unique constraint fails, but not sure.
I am wondering how I can fix this issue. I listed my model, form, and view code below
models.py
import uuid
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone as django_timezone
class UserAgreedToLegal(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
agreed_first_terms_of_service = models.BooleanField(default=False, blank=False, null=False)
date_agreed = models.DateField(null=True, default=django_timezone.now)
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
def __str__(self):
return self.user.username
forms.py
from django import forms
from legal.models import UserAgreedToLegal
class TermsOfServiceAgreementForm(forms.ModelForm):
class Meta:
model = UserAgreedToLegal
fields = [
'agreed_first_terms_of_service'
]
views.py
if request.method == 'POST':
form = TermsOfServiceAgreementForm(request.POST)
if form.is_valid():
form.clean()
terms_of_service_agreement = form.save(commit=False)
terms_of_service_agreement.user = request.user
terms_of_service_agreement.save()
The result is
django.db.utils.IntegrityError: UNIQUE constraint failed: legal_useragreedtolegal.user_id
I think this might be because I have a OneToOneField and Django is trying to save to UserAgreedToLegal and User Model but the User model already has that ID, so the unique constraint fails, but not sure.
You are correct, a OneToOneField is in essence a ForeignKey with unique=True. It thus makes no sense to visit this view a second time.
You can check if the person already has agreed to the legal terms, and if that is the case redirect to a page, for example a view that renders a page to explain that the user already agreed, so something like:
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
#login_required
def my_view(request):
if UserAgreedToLegal.objects.filter(user=request.user).exists():
return redirect('name-of-some-view')
if request.method == 'POST':
# …
# …
where the name-of-some-view should point to a view that will render that page.
The reason it was failing was because there was a OneToOne relationship with the django user model, and when I called form.save() it was trying to save a new row (insert a new user) into both User and my model and then it would see that this user_id already exists in my model and it would tell me that it cannot be done because then it would violate a rule that I placed of having only a one to one relationship because if it did save each one user would have many records. That would create a one to many instead of a one to one.
Instead, what I needed to do was to tell django that I don't want to insert a new record in my model, or to update it if that record already exists and if not create it while maintaining its relationship with the User model.
I had to pass in an instance for Django Forms to know that I already have this model and I don't want to update this instance.
here is the code that worked for me
if request.method == 'POST':
my_user = UserAgreedToLegal.objects.get_or_create(user=request.user)[0]
form = TermsOfServiceAgreementForm(request.POST, instance=my_user)
if form.is_valid():
form.clean()
terms = form.save(commit=False)
terms.user = request.user
terms.save()
How to add username of currently logged in user to field in my model? For example, I need to store info about user like name, email and so on in model, other than default Django user model, but I still use default one to store credentials. I want to establish relationship between those, so I created username field in my model. How do I fill it with current user's username upon saving the corresponding form?
My model
class ApplicantProfile(models.Model):
name = models.CharField(max_length = 50)
dob = models.DateField()
email = models.EmailField()
description = models.TextField()
username = <something>
What do I change <something> with?
My form
class ApplicantProfileEdit(forms.ModelForm):
class Meta:
model = ApplicantProfile
fields = [
'name',
'dob',
'email',
'description',
]
My view
def ApplEditView(request):
form = ApplicantProfileEdit(request.POST or None)
if form.is_valid():
form.save()
form = ApplicantProfileEdit()
context = {
'form':form
}
return render(request, "applProfileEdit.html", context)
P.S. I tried to import models straight to my views.py, and assign request.user.username to username field of the model in my view, but it didn't work, just left that field empty. I had username as CharField when I tried this.
It is not a good idea to save the username itself, or at least not without a FOREIGN KEY constraint. If later a user changes their name, then the username now points to a non-existing user, if later another user for example changes their username to thatusername, then your ApplicantProfile will point to the wrong user.
Normally one uses a ForeignKey field [Django-doc], or in case each ApplicantProfile points to a different user, a OneToOneField [Django-doc]:
from django.conf import settings
from django.db import models
class ApplicantProfile(models.Model):
name = models.CharField(max_length = 50)
dob = models.DateField()
email = models.EmailField()
description = models.TextField()
# maybe a OneToOneField
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
In the view:
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
#login_required
def appl_edit_view(request):
if request.method == 'POST':
form = ApplicantProfileEdit(request.POST)
if form.is_valid():
form.instance.user = request.user
form.save()
return redirect('some-view-name')
else:
form = ApplicantProfileEdit()
context = {
'form':form
}
return render(request, 'applProfileEdit.html', context)
Note: In case of a successful POST request, you should make a redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Note: You can limit views to a view to authenticated users with the
#login_required decorator [Django-doc].
I have created a Edit Profile form in my Django app but it doesn't save in the database.
This is the profile model:
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile', primary_key=True) #Each User is related to only one User Profile
city_search_text = models.CharField(blank=True, max_length=300)#user picks a city from autocomplete and in the view I get or create a City object
city = models.ForeignKey(City, blank=True, null=True, related_name='city') #Each User Profile must be related to one city.
prof_pic = models.ImageField(blank=True, upload_to='profile_pictures')
dob = models.DateField(blank=True, null=True)
def __str__(self):
return self.user.first_name
This is the form:
class EditProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('dob',)#I'm testing to update this field only
def save(self, commit=True):
profile = super(EditProfileForm, self).save(commit=False)
if commit:
profile.save()
return profile
This is the view:
def editprofile(request):
if request.method == 'POST':
edit_profile_form = EditProfileForm(request.POST, instance=request.user)
if edit_profile_form.is_valid():
profile = edit_profile_form.save(commit=False)
profile.save()
if 'next' in request.GET:
return redirect(request.GET['next'])
else:
print (profile_form.errors)
else:
edit_profile_form = EditProfileForm(instance=request.user.profile)
return render(request, 'excurj/editprofile.html', {'edit_profile_form':edit_profile_form,})
After I submit the form it forwards me to index page okay but the values remain the same in the user's profile.
Seems like
if edit_profile_form.is_valid():
isn't getting called at all, and your data is not saved. That means your form has invalid data, and you should check for form errors to detect those.
Also, you are trying to print form errors if the request isn't POST, which makes no sense and won't help you printing form errors. Try using this way;
if edit_profile_form.is_valid():
profile = edit_profile_form.save(commit=False)
profile.save()
else:
print (profile_form.errors)
And check your form for errors.
I figured it out eventually. In the view I should have passed an instance of the profile not the User object. So it needs to be like this:
edit_profile_form = EditProfileForm(request.POST, instance=request.user.*profile*)