File upload form in Django - python

I'm trying to create a simple upload form for my project in Django 1.5 an Python 2.7.
This is my File class:
class File(models.Model):
middleschool = 'MS'
highschool = 'HS'
university = 'U'
blank = '-'
school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
name = models.CharField(max_length = 30, primary_key=True, blank=False, null=False)
description = models.CharField(max_length = 140, blank=False, null=False)
school = models.CharField(max_length = 30, choices = school_choices, default = blank)
subject = models.ForeignKey(Subject)
user = models.ForeignKey(User)
rating = models.DecimalField(max_digits=2, decimal_places=0, default = 0)
price = models.DecimalField(max_digits=2, decimal_places=1, default = 0, blank=True, null=True)
file = models.FileField(upload_to= "/file/")
this is the form:
class UploadFileForm(forms.Form):
middleschool = 'MS'
highschool = 'HS'
university = 'U'
blank = '-'
school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),)
name = forms.CharField(max_length = 30, required = True)
file = forms.FileField()
description = forms.CharField(max_length = 140, required = False, label='Breif description of the files content')
school = forms.ChoiceField(choices = school_choices, required=False, label='What level is the material that are you uploading?', initial = blank)
subject = forms.ModelChoiceField(queryset=Subject.objects.order_by('?'), required=False, label='What subject this file is about?')
price = forms.IntegerField(required=False)
this is the view:
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
new_file = File(file = request.FILE['file'])
cd = form.cleaned_data
new_file.name = cd['name']
new_file.description = cd['description']
new_file.school = cd['school']
new_file.subject = cd['subject']
new_file.price = cd['price']
new_file.rating = '0.0'
new_file.user = request.user
new_file.save()
form = Search()
return render(request, 'home.html', {'form': form, 'request': request})
else:
form = UploadFileForm()
return render(request, 'upload.html', {'form': form, 'request': request})
and this is the relative HTML
{% if request.user.is_authenticated %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Upload">
<input type="reset" value="Reset">
</form>
{% else %}
<p>You must be logged to upload a file</p>
{% endif %}
My app path is: C:/Users/User/Desktop/site_is/app_is/ and I want hose files saved in the folder: C:/Users/User/Desktop/site_is/app_is/static/file/. In my Setting.py I set:
MEDIA_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/file/'
MEDIA_URL = '/file/'
STATIC_ROOT = 'C:/Users/User/Desktop/site_is/app_is/static/'
STATIC_URL = '/static/'
The problem is: when i select the file and hit the upload button the FileField empties itself and the form raise an error since that field is required.
I fear I'm doing something wrong with the media/static paths in the Setting.py because the view syntax it's the same as the one in the Django docmentation but I really don't know how to solve this problem.

You should specify enctype in form tag to allow file upload.
<form action="" method="post" enctype="multipart/form-data">

Related

Django Models - Form saves but model is blank

Not sure my title is fully representing my problem.
I thought I would put a screenshot of the problem (admin panel), so it's clearer for everyone
It looks like the form is savings, but nothing goes inside.
Here is the models code:
class Venue(models.Model):
name = models.CharField(verbose_name="Name",max_length=100, null=True, blank=True)
address = models.CharField(verbose_name="Address",max_length=100, null=True, blank=True)
town = models.CharField(verbose_name="Town",max_length=100, null=True, blank=True)
county = models.CharField(verbose_name="County",max_length=100, null=True, blank=True)
post_code = models.CharField(verbose_name="Post Code",max_length=8, null=True, blank=True)
country = models.CharField(verbose_name="Country",max_length=100, null=True, blank=True)
longitude = models.CharField(verbose_name="Longitude",max_length=50, null=True, blank=True)
latitude = models.CharField(verbose_name="Latitude",max_length=50, null=True, blank=True)
city = models.CharField(max_length=120)
def __str__(self):
return str(self.name) if self.name else ''
Obviously, I am aware I have asked to return '' if self.name wasnt there. The reason why I did it, is because initially, the models was visible on the admin panel under "-" but was throwing an error message when clicking on it.
Considering I am working with a form, here is the form code:
class VenueForm(forms.ModelForm):
name = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
address = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
town = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
county = forms.CharField(max_length=100, required=True, widget = forms.HiddenInput())
post_code = forms.CharField(max_length=8, required=True, widget = forms.HiddenInput())
country = forms.CharField(max_length=40, required=True, widget = forms.HiddenInput())
longitude = forms.CharField(max_length=50, required=True, widget = forms.HiddenInput())
latitude = forms.CharField(max_length=50, required=True, widget = forms.HiddenInput())
phone = forms.CharField(max_length=120)
web = forms.URLField(max_length=120)
email_address = forms.CharField(max_length=120)
class Meta:
model = Venue
fields = ['name', 'address', 'town', 'county', 'post_code','country','post_code','latitude','city', 'web', 'phone', 'email_address']
the views
def add_venue(request):
submitted = False
form = VenueForm()
if is_ajax(request) and request.method =="POST":
form = VenueForm(data = request.POST)
if form.is_valid():
form.save()
messages.success(request,("Success!"))
return HttpResponseRedirect('/add_venue?submitted=True')
context = {
'form' : form,
'submitted' : submitted,
'google_api_key' : settings.GOOGLE_API_KEY,
'base_country' : settings.BASE_COUNTRY,
}
return render(request,"main/add_venue.html",context)
and finally the html file
<div class="form-group">
<input type="text" placeholder="*Begin typing address" id="id-google-address" name="google_address">
<form id="venueform" method="POST" action="{% url 'add_venue'%}">
{% csrf_token %}
<label for="name" class="hidden-el" hidden>Name</label>
{{form.name}}
<label for="address" class="hidden-el" hidden>Address</label>
{{form.address}}
<label for="town" class="hidden-el" hidden>Town/City</label>
{{form.town}}
<label for="county" class="hidden-el" hidden>County</label>
{{form.county}}
<label for="post_code" class="hidden-el" hidden>Postal Code</label>
{{form.post_code}}
<label for="country" class="hidden-el" hidden>Country</label>
{{form.country}}
<label for="longitude" class="hidden-el" hidden>Longitude</label>
{{form.longitude}}
<label for="latitude" class="hidden-el" hidden>Latitude</label>
{{form.latitude}}
<h4>Phone</h4>
<label for="phone" class="hidden-el" hidden>Phone</label>
{{form.phone}}
<h4>WebSite</h4>
<label for="web" class="hidden-el" hidden>Website</label>
{{form.web}}
<h4>Email Address</h4>
<label for="email_address" class="hidden-el" hidden>Email Address</label>
{{form.email_address}}
<button type = "submit" class="btn btn-secondary">Add Venue</button>
</form>
</div>
{% endblock %}
{% block extend_footer %}
<script type="text/javascript">
var google_api_key = "{{google_api_key|safe}}";
var base_country = "{{base_country|safe}}";
</script>
<script src="{% static 'google_places.js' %}"></script>
{% endblock %}
def add_venue(request):
submitted = False
form = VenueForm()
venue = Venue.objects.create() # <- This line is the issue.
You are creating an empty venue, and not going anything with the variable.
Then, you are saving the form
data = form.save(commit = False)
data.name = form.cleaned_data['name']
data.address = form.cleaned_data['address']
data.town = form.cleaned_data['town']
data.county = form.cleaned_data['county']
data.post_code = form.cleaned_data['post_code']
data.country = form.cleaned_data['country']
data.longitude = form.cleaned_data['longitude']
data.latitude = form.cleaned_data['latitude']
data.city = form.cleaned_data['city']
data.phone = form.cleaned_data['phone']
data.web = form.cleaned_data['web']
data.email_address = form.cleaned_data['email_address']
data.save()
Without having validated the form first.
You need to call form.is_valid() before any of this.
Then, you are validating the form, and saving the form again.
You are trying a single object, 3 times. 2 of which are executed wrong.
Then, you are not instantiating your form in the
else:
form = VenueForm
if 'submitted' in request.GET:
submitted = True
And on top of that, you should return the form POSTed, so you can send back the form.errors().
else:
form = VenueForm()
if 'submitted' in request.GET:
submitted = True
If this is a ModelForm, all that is needed is:
def add_venue(request):
submitted = False
form = VenueForm()
if is_ajax(request) and request.method =="POST":
form = VenueForm(data = request.POST)
if form.is_valid():
form.save()
messages.success(request,("Success!"))
return HttpResponseRedirect('/add_venue?submitted=True')
context = {
'form' : form,
'submitted' : submitted,
'google_api_key' : settings.GOOGLE_API_KEY,
'base_country' : settings.BASE_COUNTRY,
}
return render(request,"main/add_venue.html",context)

How to Update ImageField in Django?

i am new in Django. i am having issue in updating ImageField.i have following code
in models.py
class ImageModel(models.Model):
image_name = models.CharField(max_length=50)
image_color = models.CharField(max_length=50)
image_document = models.ImageField(upload_to='product/')
-This is My forms.py
class ImageForm(forms.ModelForm):
class Meta:
model = ImageModel
fields = ['image_name', 'image_color' , 'image_document']
in Html file (editproduct.html)
<form method="POST" action="/myapp/updateimage/{{ singleimagedata.id }}">
{% csrf_token %}
<input class="form-control" type="text" name="image_name" value="{{ singleimagedata.image_name}}">
<input class="form-control" type="file" name="image_document">
<button type="submit" class="btn btn-primary">UPDATE PRODUCT</button>
</form>
-myapp is my application name. {{singleimagedata}} is a Variable Containing all fetched Data
-urls.py
urlpatterns = [
path('productlist', views.productlist, name='productlist'),
path('addproduct', views.addproduct, name='addproduct'),
path('editimage/<int:id>', views.editimage, name='editimage'),
path('updateimage/<int:id>', views.updateimage, name='updateimage'),
]
and Here is My views.py
def productlist(request):
if request.method == 'GET':
imagedata = ImageModel.objects.all()
return render(request,"product/productlist.html",{'imagedata':imagedata})
def addproduct(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
messages.add_message(request, messages.SUCCESS, 'Image Uploaded')
return redirect('/myapp/productlist')
else:
imageform = ImageForm()
return render(request, "product/addproduct.html", {'imageform': imageform})
def editimage(request, id):
singleimagedata = ImageModel.objects.get(id=id)
return render(request, 'product/editproduct.html', {'singleimagedata': singleimagedata})
def updateimage(request, id): #this function is called when update data
data = ImageModel.objects.get(id=id)
form = ImageForm(request.POST,request.FILES,instance = data)
if form.is_valid():
form.save()
return redirect("/myapp/productlist")
else:
return render(request, 'demo/editproduct.html', {'singleimagedata': data})
My image Upload is working fine.i can not Update image while updating data.rest of the data are updated.i don't know how to update image and how to remove old image and put new image into directory.
I think you missed the enctype="multipart/form-data", try to change:
<form method="POST" action="/myapp/updateimage/{{ singleimagedata.id }}">
into;
<form method="POST" enctype="multipart/form-data" action="{% url 'updateimage' id=singleimagedata.id %}">
Don't miss also to add the image_color field to your html input.
Because, in your case the image_color field model is designed as required field.
To remove & update the old image file from directory;
import os
from django.conf import settings
# your imported module...
def updateimage(request, id): #this function is called when update data
old_image = ImageModel.objects.get(id=id)
form = ImageForm(request.POST, request.FILES, instance=old_image)
if form.is_valid():
# deleting old uploaded image.
image_path = old_image.image_document.path
if os.path.exists(image_path):
os.remove(image_path)
# the `form.save` will also update your newest image & path.
form.save()
return redirect("/myapp/productlist")
else:
context = {'singleimagedata': old_image, 'form': form}
return render(request, 'demo/editproduct.html', context)
I had a similar issue while updating the profile_pic of user. I solved this with the following code I think this might help:
Models.py
class Profile(models.Model):
# setting o2o field of user with User model
user_name = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
first_name = models.CharField(max_length=70, null=True, blank=True)
last_name = models.CharField(max_length=70, null=True, blank=True)
profile_pic = models.ImageField(upload_to="images", blank=True, null=True,)
def __str__(self):
return str(self.user_name)
forms.py
class ProfileEditForm(ModelForm):
class Meta:
model = Profile
fields = '__all__'
# excluding user_name as it is a one_to_one relationship with User model
exclude = ['user_name']
views.py
#login_required(login_url='login')
def edit_profile(request, id):
username = get_object_or_404(Profile, id=id)
extended_pro_edit_form = ProfileEditForm(instance=username)
if request.method == "POST":
extended_pro_edit_form = ProfileEditForm(request.POST, request.FILES, instance=username)
if extended_pro_edit_form.is_valid():
extended_pro_edit_form.save()
next_ = request.POST.get('next', '/')
return HttpResponseRedirect(next_)
context = {'extended_pro_edit_form': extended_pro_edit_form}
return render(request, 'edit_profile.html', context)
edit-profile.html
<form action="" method="post"
enctype="multipart/form-data">
{% csrf_token %}
{{ extended_pro_edit_form.as_p }}
{{ extended_pro_edit_form.errors }}
<!--To redirect user to prvious page after post req-->
<input type="hidden" name="next" value="{{ request.GET.next }}">
<button type="submit">UPDATE</button>
</form>
Answer from #binpy should solve your problem. In addition to your second answer, you could do:
def updateimage(request, id): #this function is called when update data
data = ImageModel.objects.get(id=id)
form = ImageForm(request.POST,request.FILES,instance = data)
if form.is_valid():
data.image_document.delete() # This will delete your old image
form.save()
return redirect("/myapp/productlist")
else:
return render(request, 'demo/editproduct.html', {'singleimagedata': data})
Check delete() method on django docs.
some times something like cached old image is not replaced in the front-end so you might just need to forces refresh by pressing CTRL + F5 or clear your browsing history.
the answer given by #binpy is a needed update so that the files are passed to the back-end.

How to can I upload a form on django

I am trying to make a matchmaking project on python django such as snapchat's hoop, but first I am trying to do the easiest part(what I know) which is just making the user create a profile to find a mate and then show that created profile to the other users, currently I am not using any matchmaking based on questions, I need to first show the created user to other users. The error comes when I pass the variables to the html via for loops, and now the mates.html file doesnt show anything. Please if you need more explanation let me know in the comments.
models.py (Profile model show the profile of the user which is created by default wen the user is created and Mates model shows the profile that the user has to create which provide an image and a description)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.CharField(max_length=400, default=1, null=True)
class Mates(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user")
users_requests = models.ManyToManyField(User, related_name="users_requests")
req_bio = models.CharField(max_length=400)
req_image = models.ImageField(upload_to='requestmates_pics', null=True, blank=True, default=False)
views.py
def matesmain(request):
contents = Mates.objects.all()
args123 = {
'contents': contents,
}
return render(request, 'mates.html', args123)
def mates(request):
if request.method == 'POST':
form_mates = MatesForm(request.POST, request.FILES)
if form_mates.is_valid():
instance = form_mates.save(commit=False)
instance.user = request.user
instance.save()
return redirect('mates-main')
print('succesfully uploded')
else:
form_mates = MatesForm()
print('didnt upload')
return render(request, 'mates.html', {'form_mates': form_mates})
forms.py
class MatesForm(forms.ModelForm):
class Meta:
model = Mates
fields = ('req_bio', 'req_image',)
urls.py
urlpatterns = [
path('mates', views.mates, name='mates'),
path('mates-main', views.matesmain, name='mates-main'),
]
mates.html
<div class="mates">
<div class="mates-container">
{% for content in contents %}
<div class="mates-item">{{ content.user }}
</div>
{% endfor %}
<div class="mates-item">content(picture)
<form action="{% url 'mates' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form_mates.as_p }}
</form>
</div>
</div>
</div>
If you are displaying the form with the second function (mates), then you aren't sending any context for your fields to iterate over. Try like so:
def mates(request):
if request.method == 'POST':
form_mates = MatesForm(request.POST, request.FILES)
if form_mates.is_valid():
instance = form_mates.save(commit=False)
instance.user = request.user
instance.save()
return redirect('mates-main')
print('succesfully uploded')
else:
form_mates = MatesForm()
print('didnt upload')
context = {
'form_mates': form_mates,
'contents': Mates.objects.all()
}
return render(request, 'mates.html', context)
and in your other view (matesmain):
contents = Mates.objects.all()
args123 = {
'contents': contents,
'form_mates': MatesForm()
}
return render(request, 'mates.html', args123)
I'm not sure I understand, however, why you have two separate views for this.

How To style a Django formset for fileField

I have this formset.
instance = get_object_or_404(Post, user = request.user, slug = slug )
files = file.objects.all().filter(Post=instance)
FormSet = modelformset_factory(file, fields=('file',), can_delete=True)
formset=FormSet(request.POST or None, request.FILES or None, queryset=files)
models.py:
class file(models.Model):
file = models.FileField(upload_to = upload_location, blank = True, null = True)
Post = models.ForeignKey(Post, blank = True, null = True)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank = True, null = True)
def filename(self):
f = self.file.name
f1 = f.split('/', 3)[-1]
return f1
def __unicode__(self):
return self.file.name
def __str__(self):
return self.file.name
thats how i used it in my template:
{{ formset.management_form }}
{% for form in formset %}
{{form.file}}
{{form.DELETE.label}}
{{form.DELETE}}
{% endfor %}
and it shows like this in my browser:
i tried every this but i could not able to to get rid of currently and i literally do not know how to style this. please help me guys.

Files not uploading in Django form

So I've got a model with several fields, two of them being a photo and a video field. They are both of type FileField. I've already made a form before using the FileField type and everything worked correctly, but for some reason this time it is not. The settings.py is configured correctly, because I can upload a file through another form.
I can go into the Django admin and from there add an image and video for the photo and video field and the new directory is made and the image and video is stored and can be accessed with no problem. I'm a little stumped on this one. Thanks in advance.
Here is my model:
def get_image_path(instance, filename):
return os.path.join('images', str(instance.id), filename)
def get_video_path(instance, filename):
return os.path.join('videos', str(instance.id), filename)
class User(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
photo = models.FileField(upload_to=get_image_path, blank=True)
video = models.FileField(upload_to=get_video_path, blank=True)
def __unicode__(self):
return self.first_name + ' ' + self.last_name + ' - ' + self.email
Here is my form, which is a ModelForm:
class UserForm(forms.ModelForm):
class Meta:
model = User
And here is the view:
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
pollform = PollForm()
choiceform = ChoiceForm()
userform = UserForm()
all_users = User.objects.all()
if request.method == 'POST':
pollform = PollForm(request.POST, request.FILES)
choiceform = ChoiceForm(request.POST)
userform = UserForm(request.POST)
if pollform.is_valid():
pollform.comments = pollform.cleaned_data['comments']
pollform.save()
else:
pollform = PollForm()
if choiceform.is_valid():
choiceform.save()
else:
choiceform = ChoiceForm()
if userform.is_valid():
userform.save()
else:
userform = UserForm()
context = {'latest_poll_list': latest_poll_list, 'pollform': pollform,
'choiceform': choiceform, 'userform': userform, 'all_users': all_users}
return render(request, 'polls/index.html', context)
Here is the form associated with the ModelForm:
<form action="{% url 'polls:index' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ userform.as_p }}
<input type="submit" value="Submit">
</form>
Make sure you have enctype="multipart/form-data" as an attribute in your form tag.
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<your form code>
</form>
Some other answers (i.e. this one) indicate you need to pass request.FILES to your Form if you're using a FileField. Is this your problem?
I.e. change this line:
userform = UserForm(request.POST)
to:
userform = UserForm(request.POST, request.FILES)
EDIT:
Django docs here.

Categories