1π
Here Iβm giving two solutions. The one you asked and then my suggestion.
Use the following code to set the upload location to your templates folder.
from django.conf import settings
image=models.ImageField(upload_to = settings.TEMPLATE_DIRS[0])
The above code will upload image to your template directory.
My Suggestion:
Upload images to your media folder. Then use the MEDIA_URL
to allow users to download them. Use the following code to define them.
settings.py
MEDIA_ROOT = 'C:/ghoghnous/HubHub/media'
MEDIA_URL = 'site_media'
urls.py
from django.conf import settings
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
models.py
class SampleModel(models.Model):
image = models.ImageField(upload_to = 'images')
If you use this, the images will be uploaded to the folder C:/ghoghnous/HubHub/media/images/
. Then get your required images by using objects.get
or objects.filter
.
record = SampleModel.objects.get(id = 1)
If I print record.image
, the output will be images/filename.jpg
.
Pass this to your template. Then you can display the image or give download link as follows:
<a href="{{ record.image.url }}/" >Download</a> #Download link
<img src="{{ record.image.url }}/" /> #To display image
<a href="/site_media/images/file.jpg" >Download</a> #Download static files
I suggest you using the second method, since saving images in templates folder is not adviced.
0π
Template directory is not a preferred place to store media as per Django good practices.
Also, any images that you display on the web page will have a path and can be downloaded. Some people use scripting to stop right clicks and stuff like this but as far as I understand source code always give you image paths.
- [Answer]-How to make form field blank after submitting
- [Answer]-Do I have to use Compass to modify CSS with Django-Grappelli?
- [Answer]-Django: Foreign key optional but unique?
- [Answer]-Django admin inline conditional values
- [Answer]-Angular's ng-repeat with predefined markup
0π
Oh, I think youβd like to upload the file to a temporary folder and then do something with it. Right?
You need to override method clean_image
in forms
. Then you can write your own code with any path you want using File storage
- [Answer]-Django's admin is missing css, images, etc β unable to properly set up static files on shared host
- [Answer]-Make django urls ignore additional params