Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:48:11+00:00 2026-06-11T07:48:11+00:00

I have a model with this field: image=models.ImageField(upload_to=’company-category’) company-category is a folder in uploads

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:48:13+00:00Added an answer on June 11, 2026 at 7:48 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have this model which has Image field to be uploaded. It has a
models.py: class Item(models.Model): name = models.CharField(max_length=50) class ItemImage(models.Model): image = models.ImageField(upload_to='item_pics') item = models.ForeignKey(Item,
I have 3 models. class Picture(models.Model) name = models.CharField(max_length=255) image_field = models.ImageField(upload_to=foo/) slug =
I'm doing this query: SomeObject.objects.annotate(something=Avg('something')).order_by(something).all() I normally have an aggregate field in my model
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
Let's say I have a model with a field based on the ImageField class.
I have a model that I want to save in Django Admin class Product(models.Model):
Background : I have a simple model form with a image field. class XYZForm(ModelForm):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.