I created a model, all fields from the model are displayed, except for the image, the server gives a photo, but it does not show in the browser, I launch other projects, everything works! what's wrong?

upd.not generally reflects the images that are locally located on the computer, only by reference from the Internet

browser

models.py

class Trademark(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) tm1 = models.ImageField(blank=True) 

settings.py

 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 

urls.py

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

page.html

 <img src="{{trademark.tm1.url}}"> 

    2 answers 2

    Instead

     url(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), 

    do as written in the documentation

     urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

    And yes, your link to media duplicated. Rewrite and static through static , and media through the static function. The rest is offhand.

    • changed as written in the documentation (cleaned the cookies and cache, deleted the media folder, made the migration), no effect! if you understand correctly, then you are saying to change the links in settings.py ??? This is what it says: raise ImproperlyConfigured ("The MEDIA_URL and STATIC_URL"
    • @SikoraYaroslav in urls you have url(r'^media/(?P<path>.*)$' And static(settings.MEDIA_URL, is the same link, delete the first one - FeroxTL
    • right? urlpatterns = [url (r '^ admin /', include (admin.site.urls)), url (r '', include ('page.urls')),] + static (settings.MEDIA_URL, document_root = settings. MEDIA_ROOT) \ + static (settings.STATIC_URL, document_root = settings.STATIC_ROOT) - Sikora Yaroslav
    • @SikoraYaroslav Yes - FeroxTL

    problem in the page.urls.py incorrectly spelled out the address