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.
Related
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.
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.
I figure the title is somewhat confusing but that's the best I came up with.
I have an upload image form which I want to process the image file further after the user upload it (with the user - cropping using jquery) rather than put it on the model imagefield right away.
Meaning, I need to save the image temporarily so I can present it to the user in a template with the jquery crop plugin.
My current solution is ugly, I made a model called TempImageToProcess which I save the image to and from that I re-present it to the user to further process.
So my question is: is there any elegant way to save image temporarily in Django?
You should check TemporaryFileUploadHandler in this section.
Look this SO question for a clean up afterwards if any unexpected error occur or validation errors.
Hope this will lead you some where.
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.
I need to display an image in a Django form. My Django form is quite simple - a single text input field. When I initialise my Django form in a view, I would like to pass the image path as a parameter, and the form when rendered in the template displays the image. Is is possible with Django forms or would i have to display the image separately?
Thanks.
If the image is related to the form itself, not to any field in particular, then you can make a custom form class and override one of as_table(), as_ul(), as_p() methods. Or you can just use a custom template instead of leaving the form to render itself.
If it is field related then a custom widget is appropriate, as Pierre suggested.
This template responsibility to display this image in your case, I believe.
If you form need to be able to send image related information (path, url..), you'll need to create a dedicated widget.