T
T
thelionsin2019-12-14 16:55:57
Django
thelionsin, 2019-12-14 16:55:57

ImageField django how to display an image?

How to use imagefield? Can someone write on the shelves? There are only old answers in Google :(
and it is desirable to create an example in which, through the admin panel, upload a picture to the page. i.e., in the admin panel, I selected a picture, and it was uploaded where you indicated.
Where to create media folders? In the same folder as manage. py? or before the manage.py folder, or in the application folder itself?))
settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os. path.join(BASE_DIR, 'media')
url
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.
models
image = models.ImageField(upload_to='images/', null=True, blank=True)
html
{% if article.image %}
--- < img src="{{article.image}}" > -- -
{% else %}
No image
{% endif %}
ps I don't upload full html, the cycle is working, everything works, the problem is only with displaying images... It
gives the following error OperationalError at /
no such column: testsite_article.image

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2019-12-14
@yuretzgt

< img src="{{article.image}}" >
replace with
< img src="{{article.image.url}}" >

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question