Django 1.9 using django sessions in two page forms - python

How can I use Django sessions to have a user be able to start a form on one page, and move to the next page and have them complete the form?
I have looked into pagination and wizard forms, but I don't get them at all.
When I have one page with a small portion of a model I'm using - in forms - and another page with the rest of the model - forms.py with the rest of the model info - I can use the first form perfectly.
But when I move to the next page, I get an error saying (1048, "Column 'user_id' cannot be null").
My best guess is to use Django sessions to fix this issue. I don't want the user to have to put in their username and password a second time to get this to work. Any ideas?
my models/forms.py:
class Contact(models.Model):
user = models.OneToOneField(User)
subject = models.CharField(max_length=100, blank=True)
sender = models.EmailField(max_length=100, blank=True)
message = models.CharField(max_length=100, blank=True)
def __str__(self):
return self.user.username
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = Contact
fields = ('username', 'password', 'email')
class ContactForm1(forms.Form):
class Meta:
model = Contact
fields = ('subject', 'sender')
class ContactForm2(forms.Form):
message = forms.CharField(widget=forms.Textarea)
class Meta:
model = Contact
fields = ('message',)
views:
def contact(request):
registered = False
if request.method =='POST':
user = UserForm(request.POST)
contact = ContactForm1(request.POST)
if user.is_valid() and contact.is_valid():
user = user.save()
user.set_password(user.password)
user.save()
contact = contact.save(commit=False)
contact.user = user
registered = True
return render(request, 'mysite/contact.html', {'user': user, 'contact': contact, 'registered': registered})
def contact_second(request):
if request.method =='POST':
contact = ContactForm2(request.POST)
if contact.is_valid():
contact = contact.save(commit=False)
contact.save()
return render(request, 'mysite/contact_two.html', {'contact': contact}

I think it's a good idea to use sessions to store the forms because you don't want on each page to store the user input into the database because what if s/he change mind in the 3rd page and s/he wants to discard the registration or whatever it is?
I think is better to store the forms in session until you get in the last page, you ensure that everything is valid and then save the data in the database.
So let's say that the bellow code is in one of the view that will serve the first form/page. You retrieve the data from the POST request and you check if the given data are valid using the form.is_valid(), you then store the form.cleaned_data (which contains the user's validated data) to a session variable.
form = CustomForm(request.POST)
...
if form.is_valid():
request.session['form_data_page_1'] = form.cleaned_data
Note here that you may need to add code to redirect the user to the next page if form.is_valid() is true, something like this:
if form.is_valid():
request.session['form_data_page_1'] = form.cleaned_data
return HttpResponseRedirect(reverse('url-name-of-second-page'))
Then in any other view let's say in the view that is going to serve the second page you can retreive the from data from the first page like this:
first_page_data = request.session['form_data_page_1']
And you can do whatever you want with them as if it was after you executed the form.is_valid() in the first view.

Related

How to automatically get user in django admin through form

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 !!!

Field 'id' expected a number but got '-'

I'm basically new to the Django and got such ValueError.
I have a basic user model inheriting from AbstractUser class.
Also I have Profile model (in separate app), which also contain OneToOneField refering to my User model.
First of all, lets take a look on my Profile model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
country = models.ForeignKey('Country', on_delete=models.SET_DEFAULT, default='-')
full_name = models.CharField(max_length=32, default='-')
about = models.TextField(max_length=1000, default="User don't set any information about")
register_date = models.DateField(auto_now=True)
image = models.ImageField(upload_to='images/', default='images/no_avatar.png')
active = models.BooleanField(default=False)
website = models.URLField(default='-')
github = models.URLField(default='-')
twitter = models.URLField(default='-')
instagram = models.URLField(default='-')
facebook = models.URLField(default='-')
Secondly when I create a new User I have to also create his own profile (model instance refering to the user instance).
Let's look on my view.py where I creating a new user.
class SignupView(View):
def get(self, request):
"""
:param request:
:return: Register page
"""
return render(request, "login/register.html", {
'form': SignupForm()
})
def post(self, request):
"""
Tests data validity,creating user if everything is ok.
:param request:
:return: Particular user page if data is valid, otherwise it will return couple of mistake messages
"""
form = SignupForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password1 = form.cleaned_data['password']
password2 = form.cleaned_data['confirm_password']
if password1 == password2:
try:
user = User.objects.create_user(username=username, email=email, password=password1)
profile = Profile.objects.create(user=user)
user.save()
profile.save()
return HttpResponseRedirect(reverse('myprofile:index', args=(user.id, )))
except IntegrityError:
return render(request, 'login/register.html', {
'message': 'Username is already taken',
'form': form
})
else:
return render(request, 'login/register.html', {
'message': 'Passwords must match',
'form': form
})
return render(request, 'login/register.html', {
'form': form
})
I left the whole class based view but you likely need to check post function out.
And this is where the problem begins.
I getting an error on "profile = Profile.objects.create(user=user)" line. On this moment Django stopping an execution and raising an ValueError with "Field 'id' expected a number but got '-'.
".
What I already have tried?
Remove a database and also remove migrations by "python3 manage.py migrate app zero --fake" and then reapplying migrations to a new database.
So I don't probably think this is a problem with migration (not sure).
But what did I miss?
I will be very grateful for any help.

How do I add username of logged in user to model field in django

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].

Can't update user profile in my Django app Django

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*)

Django form validation: required fields not displaying errors when blank

im trying to build a simple form with some simple validation rules.
the problem i am having is that fields that are supposed to be required are not causing any validation prompts to appear in the form page when i try to submit the form. As in, when i submit a completely blank form, i get redirected to my specified page instead of having any valifation error promts appear.
The form does not actually any data to the database though, so it looks like the is_valid function is working fine, and if i enter a string where an integer should be, i get my error messages ok. So im wondering if you normally have to do some extra coding to have some 'field required' messages appear. currently i am letting django take care of all the validation, and i am not creating any custom rules. heres some code:
the model
class Advert(models.Model):
user = models.ForeignKey(User)
category = models.ForeignKey('Category')
title = models.CharField(max_length=100, null=False, blank=False)
description = models.TextField()
price = models.IntegerField(null=False, blank=False)
image = models.FileField(upload_to=get_upload_file_name, null=True)
location = models.CharField(max_length=50, null=False, blank=False)
pub_date = models.DateField(auto_now_add=True)
something i want to note is that after adding these to the model, makemigrations did not detect the changes, but i think its assumed that null=False when not explicity specified? and in the ModelForm, required=True is assumed also no?
the form
class AdvertForm(forms.ModelForm):
class Meta:
model = Advert
exclude = ['user']
the view file
def create_advert(request):
if request.POST:
form = AdvertForm(request.POST, request.FILES)
if form.is_valid():
ad = form.save(False)
ad.user = request.user
ad.save()
return HttpResponseRedirect('/adverts/list_adverts/0/')
else:
c = {}
c.update(csrf(request))
c['form'] = AdvertForm()
return render(request, 'create_advert.html', c)
so is_valid is working, the record is not saved, but im getting no error prompts for leave required fields blank.
is there something im missing?
Because you're redirecting even if the form is not valid. Instead, you should stay on the same view and re-render the form. The view should look like this:
def create_advert(request):
if request.POST:
form = AdvertForm(request.POST, request.FILES)
if form.is_valid():
ad = form.save(commit=False)
ad.user = request.user
ad.save()
return HttpResponseRedirect('/adverts/list_adverts/0/')
else:
form = AdvertForm()
return render(request, 'create_advert.html', {'form': form})
(Note there's no point in manually adding the CSRF token, the context processors will do so since you are using the render shortcut.)

Categories