Django; What is preferable way to upload some images? - python

I've been working on Django project. Currently, user can upload only one image but now I want to change it so user can upload some images.
models.py is like this.
class Entry(models.Model):
photo = models.ImageField(...)
I am thinking of just adding photo2 and photo3 inside Entry model. But I'm wondering if there's a better way to do it. I don't want to delete images that are already uploaded. Anyone who could give me tips? Also, I don't like the file upload form's design and some people use just button-like form. I also want to know how to create button-like form.

Create new model called 'Photo'.
Create relationship from 'Entry' to 'Photo'.
If you want to use photos that related with Entry record, you need to use select_related() function
Also you can find button-like upload form here.

Related

What are the ways to make it possible to change banner image of site directly through django admin?

I know how to make it possible to create/edit/delete a multiple-row database model through django admin page. But let's say I want to make it possible to modify the single banner image of my site through admin page, what am I supposed to do then?
Should I create an entire database model for one item? Or is there a better way to implement this?
You can either create your own model for these types of settings, or you can use one of the many packages designed to handle this type of situation:
https://djangopackages.org/grids/g/live-setting/

Django can StdImageField replace default variation?

I have existing Django-based project with some files uploaded.
I need to add a feature to automatically resize new uploaded files to some resolution (200x200). I found a nice library django-stdimage that does what I need.
But on upload it stores original file with its original resolution. And existing code works with the original file instead of resized one.
class Product(models.Model):
name = models.CharField(verbose_name=_('Name'), max_length=64)
image = StdImageField(upload_to='product_images/', verbose_name=_('Image'), blank=True, null=True,
variations={'default': (200, 200)})
I would like to save processed files by the same name as original file. I do not need original file by the way.
I do not want to change all the code where it works with image field - there are complex DRF serializers, some views, forms, templates, etc.
So I would like to get new resized image as before by using myproduct.image - in templates for example.
Is it possible to do without subclassing StdImageField ?
I opened a ticket on the StdImage's page and received an answer..
No, I'm sorry, but nether would this work, nor would I advice you to
do such a thing. It would add implicit behavior. That's something you
might want to avoid long term.
You can always overwrite the field tho and implement your own
behavior. The StdImage code base should be good guidance to implement
your own behavior.

How to do a user image upload - Django

I really need a lot of help with this and I dont know where to start since I am new to Django so excuse my ignorance.
I'll explain. I am building kind of a image uploading-based page. The images are grouped into albums. I created the album model and the image model which belongs to an album and it has its foreign key. But I dont know how to do the upload itself. I thought I just had to handle the data taken from the template in the views.py file and kind of upload it; but I am getting confused with some things I saw about a form... I dont know. I have no idea.
So its simple: How do I upload the image and register a new row in the database, submitting all the additional data (like pic description) the module requires? Please help with this.
Looks like you are confuse about django forms. If you define the forms.py and simply do {{ form }} will render all the field in html so you don't need to worry about defining in html form field. or Don't use form... Define all the field by yourself (!mportant Client side validation is must )
Write the views like save the file into Image Table that's it. If you need anyhelp here . Happy to assist you ..

Django Temporary Images

Hi I have this type of model
class Post(models.Model):
title = models.CharField()
image = models.ImageField()
# other fields
For creating a new post I would a flow like this:
in the new post page 1 the user choose the photo and upload it
(submit)
in the new post page 2 the user can see the photo and fill other fields like 'title'
My question is how to upload a photo and display it in the next page, without creating a new Post object with the image field. Should I save the image in a temporany directory? In this case, how can I keep the reference and use it in the second page?
Thanks.
There are couple of solutions
Define another model to just hold images, and add may be OneToOneField in your Post model. On submitting page1, create instance of this image model and put it in page2 as image field.
With html5 file API, you can refer to local file in image field. So you can show image on page2 using local client side path w/o storing it at server. refer: View image selected from file-system on client-side before upload?
First read the relevant doc so you understant what happens with uploaded files:
https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
The simplest solution IMHO would be to
1/ save the uploaded file in some temporary directory - just make sure you can serve the content one way or another (either directly thru the front webserver, or thru a custom view if you want more control on who can access this content),
2/ pass the temporary file path and url to the "next" view one way or another (hidden form fields, session...).
I use to have table to store images (any files in fact :)). Each image is linked to object (FK) and has status - Draft, Approved, Deleted. So I don't use temporary directory, just change status in table of images.

Django Custom image upload field with dynamic path

I'm trying to create my own field type for uploading images, it won't be used in the django admin panel. I imagine the syntax would probably look something like:
photo = CustomImageField(upload_to='images', width="200", height="200")
I want to be able to set the parameters in the field when I upload and save an image. I've looked through the django documentation but I can't find anything about how to do this. So basically, I want to be able to control where the image is uploaded to and the width and height parameters when the upload form is processed and the object is saved.
I am using Django 1.2
Any help would be greatly appreciated :)
You don't need a custom field for this. As the documentation shows, upload_to can be a callable, which is passed the instance and returns a path which determines where the field is saved.
I'm not sure what you mean about the width and height parameters. These are calculated from the image in any case. If you specify height_field and width_field in the field definition, the corresponding fields will be filled with the values of the image's height and width when the image is uploaded.
Your can change the storage of a FileField using the location property.
form_name.photo.storage.location = upload_dir_path
It works for the FileField and maybe it will also work for your custom image field if you inherit the Django's ImageField.
all info for uploading:
http://docs.djangoproject.com/en/dev/topics/files/
and info on resizing
resize image on save
i'll hope it helps
EDIT: all you need i think is: http://code.google.com/p/django-stdimage/
but i don't know if it works on current version.

Categories

Resources