I am trying to allow user to upload several pictures, but only one picture is saving to the database. This is my Model
from django.db import models
from django.contrib.auth.models import User
class File(models.Model):
files = models.FileField(upload_to='images/')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='files')
next, the form for the upload looks like this:
from django import forms
from .models import File
class FileForm(forms.ModelForm):
class Meta:
model=File
fields=('files',)
widgets={'files':forms.FileInput(attrs={'id':'files','required':True,'multiple':True})}
finally, the view 'upload pics' is the following:
def upload_pics(request, user_id):
if request.method == "POST":
form = FileForm(request.POST, request.FILES)
files = request.FILES.getlist('file_field')
if form.is_valid():
pics = form.save(commit=False)
pics.user = request.user
pics.files = request.FILES[files]
return redirect("groups:index")
else:
form = FileForm()
render(request, 'accounts/account_form.html', {'form':form})
return render(request, 'accounts/account_form.html', {'form':form})
the problem is that only 1 picture is being saved. any help is really appreciated!
Your code looks fine, but it seems like what you are looking for is Django Formsets. It will allow you to upload multiple images at once. Also take a look at the answer to this question: how to upload multiple images to a blog post in django
Related
I want to upload multiple files through a ModelForm,with all files to be assigned to a file field of the Model.I have gone through the docs and I saw an example on it and I ve implemented it here but I can only get my form to pick multiple files but only one get saved and assigned to filesfield.Below are my codes
models.py
class Feed(models.Model):
user=models.ForeignKey(User,on_delete=models.CASCADE,related_name='feeds')
text=models.TextField(blank=False,max_length=500)
files = models.FileField(upload_to="files/%Y/%m/%d")
forms.py
class FeedForm(ModelForm):
class Meta:
model=Feed
fields=('text','auth','files')
widgets={"files":forms.FileInput(attrs={'id':'files','required':True,'multiple':True})}
and views.py
def post_feed(request):
form_class = FeedForm
if request.method == 'POST':
form = form_class(request.POST,request.FILES)
if form.is_valid():
feed = form.save(commit=False)
feed.user = User.objects.get(pk=1)
feed.pub_date=timezone.now()
#instance = Feed(files=request.FILES['files'])
# feed.files=request.FILES['files']
feed.save()
return redirect('home')
else:
form = form_class()
return render(request, 'post_feed.html', {'form': form,})
from django.views.generic.edit import FormView
from .forms import FeedForm
class FileFieldView(FormView):
form_class=FeedForm
template_name='post_feed.html'
'''success_url=??? #I dont know what to write here.I thought of putting this
render(request, 'post_feed.html', {'form': form,}) because I just want
to reload the page but it gave an error,so I removed it entirely.'''
def post_feed(self,request,*args,**kwargs):
form_class=self.get_form_class()
form=self.get_form(form_class)
filez=request.FILES.getlist('files')
if form.is_valid():
for f in filez:
f.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
Kindly help me out,Thanks in advance.
You have to create a separate model for the files and connect them with a foreign key:
class Feed(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE, related_name='feeds')
text=models.TextField(blank=False, max_length=500)
class FeedFile(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name='files')
I hope this helps.
Phew, it took me a whole day to figure out this. My goal was to assign multiple files to one instance of a class, like a Blog instance can have multiple Images. First things first, you cannot do this with one models.FileField inside a model (for example inside Blog class), because this field was not designed to save multiple files. So the solution is to create separate model for the files and connect them with One-to-Many Relationship (Foreign Key) as it was answered by #Carlos Mermingas.
Enough words, here is the code for the above situation:
# models.py
class Feed(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
text=models.TextField(blank=False, max_length=500)
class FeedFile(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")
feed = models.ForeignKey(Feed, on_delete=models.CASCADE)
# forms.py
...
from django.forms import ClearableFileInput
...
class FeedModelForm(forms.ModelForm):
class Meta:
model = Feed
fields = ['text']
class FileModelForm(forms.ModelForm):
class Meta:
model = FeedFile
fields = ['file']
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
# widget is important to upload multiple files
# views.py
from .models import FeedFile
...
def create_to_feed(request):
user = request.user
if request.method == 'POST':
form = FeedModelForm(request.POST)
file_form = FileModelForm(request.POST, request.FILES)
files = request.FILES.getlist('file') #field name in model
if form.is_valid() and file_form.is_valid():
feed_instance = form.save(commit=False)
feed_instance.user = user
feed_instance.save()
for f in files:
file_instance = FeedFile(file=f, feed=feed_instance)
file_instance.save()
else:
form = FeedModelForm()
file_form = FileModelForm()
# the rest is the basic code: template_name, context, render etc.
# in your template.html <form> tag must include enctype="multipart/form-data"
Bonus: if you want to see uploaded files in admin panel, you can use InlineModelAdmin objects. Here is the code:
# admin.py of your app
from django.contrib import admin
from .models import Feed, FeedFile
class FeedFileInline(admin.TabularInline):
model = FeedFile
class FeedAdmin(admin.ModelAdmin):
inlines = [
FeedFileInline,
]
admin.site.register(Feed, FeedAdmin)
For the more details on file upload, Model Forms, how to include widget in Model Form
Would suggest using an M2M field from Feed model to FeedFile model.
Makes it all the more easier while querying for files of a particular Feed object, which i feel is also the most common usecase for Feed objects
class Feed(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE, related_name='feeds')
text=models.TextField(blank=False, max_length=500)
files=models.ManyToManyField(FeedFile)
class FeedFile(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")
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',)
I have the following models:
class Picture(models.Model):
title = models.CharField(max_length=255)
(etc)
And
class Report(models.Model):
complaint = models.TextField()
picture = models.ForeignKey('Picture')
What i'd like to be able to do is
a) Include the report ModelForm in the 'Picture' ModelView template
b) Prepopulate the picture field in the report Modelform with the relevant picture
I've been messing around with contextprocessors, inclusiontags and trying to pass the variable through a querystring to a new page, but they all seem quite complex and to not work correctly.
What I'd like help with is understanding the most 'django' way to do this, and any pointers to getting this done.
Solution
With Scott's help, the working solution is:
views.py
def picture(request, slug):
picture = Picture.objects.get(slug=str(slug))
d = dict(picture=picture, form=ReportPicture())
d.update(csrf(request))
if request.method == 'POST':
form = ReportPicture(request.POST)
if form.is_valid():
report = form.save(commit=False)
report.picture = picture
report.save()
return redirect(picture.get_absolute_url())
return render_to_response("picture_detail.html", d)
I believe you are wanting inline formsets (docs).
# models.py
class Picture(models.Model):
title = models.CharField(max_length=255)
class Report(models.Model):
complaint = models.TextField()
picture = models.ForeignKey('Picture')
# views.py
from django.forms.models import inlineformset_factory
from .models import Picture, Report
def manage_picture(request, pk):
picture = Picture.objects.get(pk=pk)
ReportInlineFormSet = inlineformset_factory(Picture, Report)
if request.method == "POST":
formset = ReportInlineFormSet(request.POST, request.FILES, instance=picture)
if formset.is_valid():
formset.save()
return HttpResponseRedirect(picture.get_absolute_url())
else:
formset = ReportInlineFormSet(instance=picture)
return render_to_response("manage_picture.html", {"formset": formset})
If you're using Class Based Views Django doesn't natively offer any inline formset support but there is an excellent app that handles most of this for you called django-extra_views.
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.
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'))
...