Reputation: 91
I tried to hardcode path and nothing worked. What's wrong with my code? I'm getting 404 error and image isn't showing up.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'blog/media')
models.py
image = models.ImageField(upload_to = 'media/', blank = True)
urls.py
urlpatterns = [...] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
forms.py class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('author','title', 'text','image')
widgets = {
'title': forms.TextInput(attrs={'class': 'textinputclass'}),
'text': forms.Textarea(attrs={'class': 'editable medium-editor-textarea postcontent'}),
}
html
<img src="{{post.image.url}}">
Upvotes: 1
Views: 76
Reputation: 3002
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
from what I understand you are trying to make the django serve static files with this, static-files and media are separate, that line you have serves statics like css, js etc which resides in STATIC_ROOT
while you need to serve MEDIA_ROOT
so
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
adding this part should work, but I highly discourage using this in the actual deployment, media files should be handled by a web server like nginx and not an application server.
Upvotes: 1