I have a model with this field:
image=models.ImageField(upload_to='company-category')
company-category is a folder in uploads folder,but I want to upload images to template directory .I think this way users that visit my website can’t access images and download them.
in settings:
TEMPLATE_DIRS = (
"C:/ghoghnous/HubHub/Theme"
)
how can I do this?
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.
The above code will upload image to your template directory.
My Suggestion:
Upload images to your media folder. Then use the
MEDIA_URLto allow users to download them. Use the following code to define them.settings.py
urls.py
models.py
If you use this, the images will be uploaded to the folder
C:/ghoghnous/HubHub/media/images/. Then get your required images by usingobjects.getorobjects.filter.If I
print record.image, the output will beimages/filename.jpg.Pass this to your template. Then you can display the image or give download link as follows:
I suggest you using the second method, since saving images in templates folder is not adviced.