Django using a form with an ImageField and a User - django

In Django, the user can upload a comment with the image.
from sorl.thumbnail import ImageField
class Comment(models.Model):
count_votes = models.Integer(default=0)
user = models.ForeignKey(User)
thumb = ImageField(upload_to="thumbnails")
# ...
This is what I am trying to do :
# views.py
def add_comment(request):
if request.method == 'POST' and request.user.is_authenticated():
comment = Comment(user=request.user)
form = CommentForm(request.POST, request.FILES, instance=comment)
if form.is_valid():
form.save()
# ...
# forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
But there are some errors :
none of the fields are filled
the count_votes is not defaulted to 0 as I would like to
the user is not taken into account either
the image is said to be empty too
How can I achieve that ? I have read many questions on SO and tried various other things, like fill in things in the __init__ of the form, use initial instead of instance, ...

First, make sure in your template you have enctype="multipart/form-data" in your <form> tag, otherwise the image file will not get uploaded and your form will not validate (and thus, nothing will be added to the database).
In addition, you need to fix your views. Start by using the login_required decorator so that your view is restricted to logged-in users, and then fix your form logic:
from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
#login_required
def add_comment(request):
form = CommentForm(request.POST or None, request.FILES or None)
if form.is_valid():
obj = form.save(commit=False) # create the record, but don't save it
obj.user = request.user # add the user from the request
obj.save() # now save the record
return redirect('/')
return render(request, 'template.html', {'form': form})
Finally, in your form exclude the user because you will be adding it later. In fact, your form should just have the comment and image field. You don't need to include the count_votes field because it already has a default value; unless you want the user to modify this field.
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('thumb', 'comment',)

Related

How do I use UpdateView?

I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form.
Here is my views.py:
class RegistrationView(FormView):
form_class = RegistrationForm
template_name = "register.html"
success_url = "/accounts/profile/"
def form_valid(self, form):
if form.is_valid:
user = form.save()
user = authenticate(username=user.username, password=form.cleaned_data['password1'])
login(self.request, user)
return super(RegistrationView, self).form_valid(form) #I still have no idea what this is
class UserUpdate(UpdateView):
model = User
form_class = RegistrationForm
fields = ['username', 'first_name']
template_name = "update.html"
success_url = "/accounts/profile/"
and urls.py
url(r'^create/$', RegistrationView.as_view(), name="create-user"),
url(r'^profile/(?P<pk>\d+)/edit/$', UserUpdate.as_view(), name="user-update"),
How do I properly use UpdateView?
Problem 1.
The user is not being updated because you are using the same form
(RegistrationForm) to do your updates and to create new users.
Problem 2. Forms belong in a file of their own called forms.py.
My suggested refactoring:
#forms.py
#place(forms.py) this in the same directory as views.py
class UpdateForm(forms.ModelForm):
#form for updating users
#the field you want to use should already be defined in the model
#so no need to add them here again DRY
class Meta:
model = User
fields = ('field1', 'field2', 'field3',)
#views.py
#import your forms
from .forms import UpdateForm
#also import your CBVs
from django.views.generic import UpdateView
class UserUpdate(UpdateView):
context_object_name = 'variable_used_in `update.html`'
form_class = UpdateForm
template_name = 'update.html'
success_url = 'success_url'
#get object
def get_object(self, queryset=None):
return self.request.user
#override form_valid method
def form_valid(self, form):
#save cleaned post data
clean = form.cleaned_data
context = {}
self.object = context.save(clean)
return super(UserUpdate, self).form_valid(form)
slightly elegant urls.py
#urls.py
#i'm assuming login is required to perform edit function
#in that case, we don't need to pass the 'id' in the url.
#we can just get the user instance
url(
regex=r'^profile/edit$',
view= UserUpdate.as_view(),
name='user-update'
),
You left out a lot of info so not really sure what your setup is.My solution is based on the assumption that you have Django 1.5. You can learn more about handling forms with CBVs
first: user = form.save() saves in the db the form. since there's no pk in the form it creates a new one.
what you have to do is probably to check if a user with that username exists and if not create it (for this part check google).
second: to restrict field you have to specify them in the Meta class of the Form (which you didn't show here) check this https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform.
If you are getting new objects in the database instead of updating existing ones then it is likely that you copied and pasted the template for new objects and forgot to change the form's action attribute. This should point to view that does the update either in the form of a hard-coded path or a URL tag ({% url '<name of URLconf>' object.id %).

Django: save model object of a ModelForm

I have a ModelForm:
class UploadForm(forms.ModelForm):
class Meta:
model = Image
fields = ['image']
which is based on model Image
class Image(models.Model):
def content_file_name(instance, filename):
return filename
name = models.CharField(max_length=50)
image = models.ImageField(upload_to=content_file_name)
user = models.ForeignKey(MyUser, related_name='image')
In views.py, I try to save the image name and user object (from session) along with the uploaded image to database.
form1 = UploadForm(request.POST, request.FILES)
if form1.is_valid():
image = request.FILES['image'] # image is of UploadedFile class
form1.Meta.model.name = image.name
form1.Meta.model.user = get_object_or_404(MyUser,username=request.session['user'])
form1.save()
return render(request, '16_upload01.html', context)
Problem is only the uploaded image gets saved. Error message in browser:
IntegrityError at /competition-big/big1/upload
comp_app_image.user_id may not be NULL
I confirmed this by checking on SQL command:
INSERT INTO "comp_app_image" ("name", "image", "user_id") VALUES ('', 'grey-160-100_1.png', None)
I figure that image name and user are not bounded to form1. But how can I achieve that?
EDIT
After some digging, I know I messed up with above code. Now I changed my code to this:
if form1.is_valid():
form1.cleaned_data['user'] = get_object_or_404(MyUser, username=request.session['user'])
form1.save()
But I still get null user_id error.
EDIT 2
Thanks Jacinda. Now I get this cleaner code:
if form1.is_valid():
form1.cleaned_data['user'] = request.user
form1.save()
But error null user_id remains.
If this form can only be accessed by a logged in user, use the login_required decorator, and you should always redirect after a POST. You should also read this section in the ModelForms documentation; which describes how to properly use a model form that has limited fields.
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
#login_required
def your_view(request):
form1 = UploadForm(request.POST, request.FILES)
if form1.is_valid():
image = request.FILES['image'] # image is of UploadedFile class
obj = form1.save(commit=False)
obj.name = image.name
obj.user = request.user
obj.save()
return redirect('your-view-name')
else:
return render(request, 'form.html', {'form': form1})
I think your issue is probably this line:
form1.Meta.model.user = get_object_or_404(MyUser,username=request.session['user'])
When I try and use your syntax (using the Django default django.contrib.auth) I get a KeyError on 'user'.
What I've always done when I need information about the user associated with a request is this:
username = request.user.username
Reference: https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.user
Of course, this will only work if your users are required to be logged in to upload images.

Saving the edited information in Django

I have created a form in my app where I can take details of a suer. Now i am able to edit the form but i am not able to save the data. I would like to save the changed data and move on.
It says: Adult with this User already exists.
My urls.py:
url(r'^home/editform/(?P<userpk>[^/]+)/$', 'lexuseditform', name='lexuseditform'),
url(r'^home/edited/(?P<userpk>[^/]+)/$', 'lexusedited', name='lexusedited')
My views.py:
#login_required
def lexuseditform(request,userpk):
if int(userpk) != request.user.pk:
return HttpResponseForbidden()
else:
adult = Adult(user=request.user)
if request.method == 'POST': # If the form has been submitted...
form = AdultForm(request.POST,instance=adult) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
redirect_url = reverse('lexusedited')
return HttpResponseRedirect(redirect_url) # Redirect after POST
else:
form = AdultForm(instance=adult) # An unbound form
return render(request,'lexus/lexuseditform.html', {'form': form})
#login_required
def lexusedited(request,userpk):
return render(request,'lexus/lexusedited.html')
My forms.py:
from models import Adult
from django.forms import ModelForm
class AdultForm(ModelForm):
"""
Edit Profile Information
"""
class Meta:
model = Adult
fields = ('user', 'email','fullname')
My models.py:
from django.db import models
from django.contrib.auth.models import User
class Adult(models.Model):
"""
Adult Information
"""
user = models.OneToOneField(User)
fullname = models.CharField(max_length=100)
email = models.EmailField()
def __unicode__(self):
return self.user.username
Not sure where my error is. Need some guidance.. Thanks..
Although you haven't shown the Adult model structure, I bet it has something like
class Adult(models.Model):
user = models.ForeignKey(User, unique=True)
That's why you cannot save new Adult() with the same user(name). So you have to either change the models, or to load existing Adult for the specified user:
if Adult.objects.filter(user=request.user).exists():
adult = Adult.objects.get(user=request.user) # load existing Adult
else:
adult = Adult(user=request.user) # create new Adult
But I don't know how your form and models look like.
UPDATE:
or using:
adult, is_created = Adult.objects.get_or_create(user=request.user)
A bit of a guess since you didn't post the code for your form and model, but assuming the form is a regular model form, your problem very probably comes from your Adult model having a unique constraint on User (either a OneToOneField or a ForeignKey with unique=True). Since you create a new Adult instance for the form, it violates the unique constraint. Assuming (once again) this constraint is what you want, and your view is supposed to either create a related Adult instance for the user if it doesn't yet exist or edit the existing one, you need to first check if there's an Adult instance for the user:
#login_required
def lexuseditform(request):
try:
adult = Adult.objects.get(user=request.user)
except Adult.DoesNotExist:
adult = Adult(user=request.user)
#... your code here
Also note that I removed the userpk argument and test against request.user.pk which is useless if you think about it.
Correct Version(Refering to Tisho's Answer):
My views.py:
#login_required
def lexuseditform(request,userpk):
if Adult.objects.filter(user=request.user).exists():
adult = Adult.objects.get(user=request.user) # load existing Adult
else:
adult = Adult(user=request.user) # create new Adult
if request.method == 'POST': # If the form has been submitted...
form = AdultForm(request.POST,instance=adult) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
redirect_url = reverse('lexusedited')
return HttpResponseRedirect(redirect_url) # Redirect after POST
else:
form = AdultForm(instance=adult) # An unbound form
return render(request,'lexus/lexuseditform.html', {'form': form})

Django forms and set initial value to logged in user?

I have a form like so:
from django import forms
from django.contrib.auth.models import User
from django_countries.countries import COUNTRIES
from statuses.models import Status
class StatusForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES)
mood = forms.IntegerField()
sleep_quality = forms.IntegerField()
This form is only displayed to the users who are logged in, how can I set request.user so that when the user submits this form, I can associate the form entry to them? My model looks like the following with the the user FK:
from django.db import models
from django.contrib.auth.models import User
from django_countries import CountryField
class Status(models.Model):
user = models.ForeignKey(User)
country = CountryField()
mood = models.SmallIntegerField(default=4)
sleep_quality = models.SmallIntegerField(default=4)
Here is my view for this form as well:
#login_required
def index(request, template_name="status/index.html"):
if request.method == 'POST':
postdata = request.POST
form = StatusForm(postdata)
if form.is_valid():
messages.success(request, 'Something happened, good!')
return redirect(urlresolvers.reverse('profile'))
else:
form = StatusForm()
context = RequestContext(request, { 'form': form })
return render_to_response(template_name, context)
I thought maybe I should create a hiddenfield and store request.user in there but that does not seem safe as it can easily be edited with firebug and such. Any suggestions as to how I can store request.user for this form?
Thanks!
The current user will be present in the request as request.user so you don't need to include it in the form. Instead why not leverage ModelForms as they will deal with linking your object to your form.
class StatusForm(forms.ModelForm):
country = forms.ChoiceField(choices=COUNTRIES)
# The other fields are automatically included, we just overwrite country
class Meta:
model = Status
exclude = ("user")
Then in your view:
...
form = StatusForm(request.POST):
if form.is_valid():
# Because your model requires that user is present, we validate the form and
# save it without commiting, manually assigning the user to the object and resaving
obj = form.save(commit=False)
obj.user = request.user
obj.save()
messages.success(request, 'Something happened, good!')
return redirect(urlresolvers.reverse('profile'))
...

How to update an object from edit form in Django?

Possibly a newbie question, so please bear with me.
I have a Django form that edits a certain instance of a Model. In order to know which object is being edited, I have a hidden field containing the id of the object, along with the URL containing the id.
First question: Is having the id of the object in a hidden field the right way of doing it?
My (possibly unfounded) concern with having it only as part of the url is that someone could then open the page of one object id, submit the form to another, and that object will then be overwritten. That's why I'm trying to use a hidden field.
The problem with storing the id in a hidden field is that, on validation of the form, Django complains that the object does not have an unique id (obviously).
Second question: If a unique field is part of a form, how does one tell Django to ignore the fact that that key already exists, in order to update the object?
Why don't you just use ModelForm?
# forms.py
# ...
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
# ...
def my_view(request, id):
instance = get_object_or_404(MyModel, id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return render(request, 'my_template.html', {'form': form})
See https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/#the-save-method for more details.
Update for Django 1.6 and further version
# forms.py
# ...
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
# views.py
def my_view(request, id):
instance = MyModel.objects.get(id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()
return redirect('next_view')
return direct_to_template(request, 'my_template.html', {'form': form})