Spread the contents of models.py

class Book(models.Model): ... images = models.ImageField(upload_to='images/%Y/%m/%d/', verbose_name=u'Изображение',height_field=None, width_field=None) ... 

views.py

 def BlogIndex(request): posts = Book.objects.all() template = 'html/book_index.html' context = { 'posts':posts, } return render(request,template,context) 

urls.py

 urlpatterns = [ url(r'^admin-panel/', admin.site.urls), url(r'^', include('books.urls')), url(r'^books/', include('books.urls')), url(r'^tinymce/', include('tinymce.urls')), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += staticfiles_urlpatterns() if settings.DEBUG: urlpatterns += [ url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), ] 

settings.py

 STATIC_URL = '/static/' STATIC_ROOT = '' STATICFILES_DIRS = ( os.path.join(_PATH, 'static'), ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(_PATH, 'media') 

template

 {% for b in posts %} <div class="col s2"> <div class="card radius shadowDepth1"> <div class="card__image border-tlr-radius"> <img class="border-tlr-radius" src="{% if b.images %}{{ b.images.url }}{% else %}default.img{% endif %}" alt="{{ b.name }}" title="{{ b.name }}" /> <img class="border-tlr-radius" src="{{ MEDIA_URL }}{{ b.images }}"> <img class="border-tlr-radius" src="{{ b.images }}"> <img class="border-tlr-radius" src="{{ b.images.url }}"> </div> {{ b.images }} 

It seems that everything should work fine, and the output: Screen 1

  • And where is your media folder in the project or in the application? - user208820
  • The folder is in the project - Romuald Shmidtelson
  • It's all about the url (r '^ $', include ('books.urls')), - if you put a dollar, everything is displayed. Now you need to know how to fix it. - Romuald Shmidtelson

2 answers 2

 r'^index/$' 

The main thing here is to understand the principle of forming expressions.

  • the character r at the beginning of a line is the rejection of escaping characters in a line.
  • The symbols ^ and $ signify the beginning and the end of your expression. For example, without specifying $ after index, for django these addresses will be no different:

     /index /index/images /index/images/logo 

Yes, it will call the function we need from views, but it will not pass any parameters to it.

    Corollary: In the beginning it turned out that for the home page in url templates you need to add a dollar - url (r '^ $', include ('books.urls')), django displayed pictures but began to curse on the dollar, after modification, it would seem the usual design, he gladly ate and displayed a page with pictures: url (r '^ $', views.BlogIndex),