1

I have this model Blog and using it in 'blog' subdomain created with 'django-hosts'.

My subdomains in 'hosts.py':

from django.conf import settings
from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'blog', 'blog.urls', name='blog'),
    host(r'(|www)', settings.ROOT_URLCONF, name='www'),
)

And Blog model - Note that 'title_image' field powered by 'sorl.thumbnail' and 'content' field is a 'django-ckeditor' uploadable field:

class Blog(models.Model):
     author = models.ForeignKey(settings.AUTH_USER_MODEL,
                                verbose_name=_('author'),
                                on_delete=models.CASCADE,
                                related_name='blog_author')
     title = models.CharField(verbose_name=_('title'), max_length=200)
     title_image = ImageField(verbose_name=_('title image'), blank=True)
     content = RichTextUploadingField(verbose_name=_('content'))

I've' created a simple ListView for blog that show every blog title, content and title_image to viewer:

class BlogListView(ListView):
"""Everyone can see all blogs"""
    template_name = 'blog/templates/blog/blog_list_view.html'
    model = Blog
    context_object_name = 'blogs'

And my blog.urls:

from django.urls import path

from . import views


app_name = 'blog'


urlpatterns = [
    path('', views.BlogListView.as_view(), name='blog_list_view'),
]

When I'm using my blog subdomain (eg: blog.localhost:8000/) it doesn't show any image to me whether it's the title_image or any image in django-ckeditor powered 'content' field. But when I'm not using subdomain and instead use 'blog' app as other 'URLCONF' path (eg: localhost:8000/blog/) I can see every images without any problem. Anyone knows why using subdomains, media files does not shown and how to fix it?

Ehsan B
  • 36
  • 1
  • 4

2 Answers2

2

I figured it out.

What you need to do is load the MEDIA folder in your urls.py where your hosts is pointing to.

So in your blog.urls

from django.urls import path
from django.conf import settings

from . import views

app_name = 'blog'

urlpatterns = [
    path('', views.BlogListView.as_view(), name='blog_list_view'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Rob
  • 127
  • 1
  • 2
  • 12
0

first of all in settings.py

MEDIA_DIR = BASE_DIR / 'media'

MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'

then in main project urls.py

from django.conf.urls import url
from django.views.static import serve
from django.conf import settings

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

and I would ugget in your models use this

title_image = models.ImageField(upload_to='title_images', blank=True)

if you are using app locally then this work fine if the problem is causing in production then use cloudinary

Shreyash mishra
  • 738
  • 1
  • 7
  • 30