Hi. I hope you can help. I am trying to make my first real Django project demo for a collegue. She likes knitting and stuff.
I got the site to work with generic views and model with ImageField. I also got the upload_to working. Everything is showing up on the site. The problem is I must have messed up alle the staticdirs, media_root and all that, because the images uploaded via admin end up in a weird folder and the console log says it looks for static/static/ folder. Can you help me spot where I have made something fundamentally wrong in the dir settings or the modelâs upload_to?
Dont be confused by the two different fields on the model regarding the image. It is the ImageField I would like to work and not the image_url (which was my attempt at using external image link. Worked, but I would like to learn the static stuff better).
Your media file directory and your static file directory should be two completely separate locations. The two shouldnât be mixed.
Set up one directory for STATIC_ROOT and path for STATIC_URL, and a separate directory for MEDIA_ROOT and path for MEDIA_URL. Do not mix the two of them. The static files are for âsystem-providedâ files only. The media files are for user-uploaded content.
Also, keep in mind that the upload_to attribute is relative to MEDIA_ROOT.
Depends up what youâre looking to do with it in your template. Itâs a field of a model, that has a number of attributes. See the docs at filefield and fieldfile.
Assuming post is a reference in the context to an instance of a model containing a FileField named image_file, that should work. Which of your views and templates are you trying this in?
I am sorry. I did not understand that. But this is the model (I deleted a CharField called image_url, because I dont need it anymore):
class Post(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
image_file = models.ImageField(null=True, blank=True, upload_to='images')
and the view:
class BlogDetailView(DetailView): # new
model = Post
template_name = "post_detail.html"
And whatâs being rendered in the html for <img src="{{ post.image_file.url }}"><br>?
If you open up the Django shell and run commands like:
Post.objects.get(id=1).title
Post.objects.get(id=1).image_file
Post.objects.get(id=1).image_file.url
what is the output?
(Assuming you have an entry in Post with an id = 1)
The html code on the page shows <img src="/media/images/djangoimage.png" alt="" width="500" height="600">
I canât run Django shell (AFAIK) on replit. (I am on a semi-locked work-pc)
Ok, so the html is being rendered properly. If youâre getting a 404 on that URL, then the issue is either with your MEDIA_ROOT or web server configuration - but Django is working correctly at this point.
Another thing I noticed - youâve got MEDIA_ROOT as being within your project directory. You really donât want to do that in a production environment. You want MEDIA_ROOT to be something accessible to the web server, but not part of your project-deployment tree. (In general, the same is true for STATIC_ROOT as well. These directories contain files that should be served directly by your web server and not by your application.)
I tested some more now. Maybe these info help shed some light.
In my template I tried a heap of different ways to get the image showing. Some of these as an example:
My folder structure is like this. Could you show me the right way?
ROOT FOLDER
ââââcore
ââââmedia
â ââââimages
ââââpages
â ââââmigrations
ââââstatic
â ââââcss
ââââtemplates
ââââvenvdjangodemo
I plan on using Heroku with the Heroku CLI (via Github), but I only develop locally right now to learn.
Did you see the other answers I gave above to your other questions? (sorry if I managed to split the thread)