B
B
BotaniQ_Q2017-08-25 23:32:31
Django
BotaniQ_Q, 2017-08-25 23:32:31

Can't find the image path?

Ok, that means adding images to Django.
models.py

from django.db import models


# Create your models here.
class Article(models.Model):
  class Meta: 
    db_table = "article"


  article_image = models.ImageField(null=True, blank=True, upload_to="Images")

admin.py
from django.contrib import admin
from article.models import Article

# Register your models here.

class ArticleAdmin(admin.ModelAdmin):
  fields = [ 'article_image']
  
admin.site.register(ArticleAdmin)

settings.py (part)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'<code lang="html">

</code>
MEDIA_ROOT = os.path.join(BASE_DIR, 'article/media')

article.html
{% block articles %}

<img src="{{ article.article_image.url }}">
{% endblock %}

In chrome, in the developer panel, it shows that the image is loaded from /media/Images/
Where it lies, it lies in the article/media/Images application folder, but when you try to go to 127.0.0.1:8000/media/Images/bm9ahXKaDZM.jpg
throws out urls for an error that there is no such url, how to do it correctly so that the picture is loaded for me? :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxfox, 2017-08-26
@BotaniQ_Q

In URLs.py:

from django.conf.url.static import static
from djngo.conf import settings

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

In production - configure the web server to serve static.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question