0

I have been developing my own website in Django and have come across an issue,

I have a model:

class Text_Image_Page(TrainingModulePage):
          page_num = models.IntegerField(default = 0)
          title = models.CharField(max_length=200)
          text = models.CharField(max_length=200)
          image_desc = models.CharField(max_length=200)
          image = models.ImageField(upload_to=settings.MEDIA_ROOT, default = 'null')
          training_module = models.ForeignKey(TrainingModule, related_name='text_image_pages',  null=True)
          def __str__(self):
               return "Image Page"

I am using a modelForm to create this model, and everything works fine until I try to display the model in my template.

My Media Root in my settings.py is

STATIC_URL = '/static/'
MEDIA_URL = '/superchem_media/'
STATIC_ROOT = '/home/anthonycalab/webapps/superchem_static/'
MEDIA_ROOT = '/home/anthonycalab/webapps/superchem_media/'

The images are being correctly uploaded to the media root as I can see they exist via filezilla however when I go to display my image in my template:

<img src={{t.image.url}}  alt="{{t.image_desc}}" style="width:304px;height:228px;">

It doesn't display the image, futhermore if I load the images direct link I get a 404 error with the following message:

Request Method: GET
Request URL:             http://anthonycalab.webfactional.com/home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg
Using the URLconf defined in superchem.urls, Django tried these URL    patterns, in this order:
^training_modules/
^admin/
^accounts/
^superchem\_media\/(?P<path>.*)$
^static\/(?P<path>.*)$
The current URL,      home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg,       didn't match any of these.

Any Idea's what I could be doing incorrectly?

EDIT:

The problem has been narrowed down to:

{{t.image.url}} = /home/anthonycalab/webapps/superchem_media/41WKTB25KsL._SY395__mNsiAbb.jpg

when I want

/media/[imagelink].jpg

Community
  • 1
  • 1
  • Can you show your url settings? Looks like you don't set media url http://stackoverflow.com/questions/5517950/django-media-url-and-media-root – AlmasK89 May 12 '16 at 10:05
  • `urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += staticfiles_urlpatterns(settings.STATIC_URL)` The problem is: image.url is giving me: /home/anthonycalab/webapps/superchem_media/image.jpg instead of: media/image.jpg – Anthony Calabro May 12 '16 at 10:08

2 Answers2

0

add media url in main urls.py file

from django.contrib import admin
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings


admin.autodiscover()


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('home.urls')),

]

urlpatterns += patterns('',
                        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
                        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
                        )
urlpatterns += staticfiles_urlpatterns()
  • This still gives me the same issue. {{img.url}} = /home/anthonycalab/webapps/superchem_media/img.jpg were it should just give me /media/img.jpg – Anthony Calabro May 12 '16 at 10:12
  • Is there a way to return only the image name? e.g image1.jpg – Anthony Calabro May 12 '16 at 10:17
  • did you added this code in your settings. TEMPLATE_CONTEXT_PROCESSORS = ( '-------------------' 'django.core.context_processors.media', 'django.core.context_processors.static', '-----------' ) –  May 12 '16 at 10:19
  • it will return the filename import os class File(models.Model): file = models.FileField() ... def filename(self): return os.path.basename(self.file.name) –  May 12 '16 at 10:24
0
settings.py

import os

def root(x):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)

MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    root('static'),
)


TEMPLATE_CONTEXT_PROCESSORS = (
    '-------------------'
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    '-----------'
)

urls.py 

from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings

urlpatterns += patterns('',
                        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
                        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': False}),
                        )
urlpatterns += staticfiles_urlpatterns()