3

I am getting the error code 404 for the django media files. urs.py is as follows:

 from django.conf.urls import patterns,include, url
    from django.views.static import serve

    from django.contrib import admin
    #import settings
    from django.conf.urls.static import static
    from django.conf import settings
    admin.autodiscover()

    urlpatterns = patterns('',
        # Examples:
        # url(r'^$', 'timesheet_tool.views.home', name='home'),
        # url(r'^blog/', include('blog.urls')),
        url(r'^admin/', include(admin.site.urls)),
        url(r'^welcome_page/', include('welcome_page.urls')),
        url(r'^add_status/', include('add_status.urls')),
        url(r'^view_status/', include('view_status.urls')),

    )

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

views.py:

from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
from django.template import RequestContext

def index(request):
        return render(request, 'welcome.html', {'welcome_message':"Welcome "+get_user_name()+"!!!"})

def get_user_name():
        user_name = "Brad"
        return str(user_name)

html template:

<a href="/add_status" class="addStatusButton">Add Status </a>
<a href="/view_status/?page=0&e_date=&s_date=&type=date" class="viewStatusButton">View Status </a>
<a href="#" class="viewReportButton">View Report </a>
<a href="#" class="logOutButton">Logout</a>
<div style="display: flex; justify-content:center;">
   <img src = "{{MEDIA_URL}}logo.png" width="150" height="100">
   <img src = "{{MEDIA_URL}}sheets.png" width="150" height="100">
</div>
<h2><span>DASHBOARD</span></h2>
<p>{{welcome_message}}</p>
<style>
   p    {color: green;
   adding:23px 200px;
   position: absolute;top:130px;left:100px;}
   h2 {
   width: 100%; 
   text-align: center; 
   color:#35a5f0;
   line-height: 1em;
   margin: 10px 0 20px; 
   background-color:#35a5f0;
   } 
   h2 span { 
   background:#fff; 
   padding:0 10px; 
   }
   div.dashBoard {
   color:#35a5f0;
   font-family: 'Tangerine', serif;
   position:absolute;top:300px;left:700px;
   width: 200px; 
   height: 100px; 
   overflow-y: scroll;
   }
   .addStatusButton,.viewStatusButton,.viewReportButton,.logOutButton {
   -moz-box-shadow: 0px 0px 0px 0px #3dc21b;
   -webkit-box-shadow: 0px 0px 0px 0px #3dc21b;
   box-shadow: 0px 0px 0px 0px #3dc21b;
   background-color:#35a5f0;
   -moz-border-radius:35px;
   -webkit-border-radius:35px;
   border-radius:28px;
   border:1px solid #ffffff;
   display:block;
   cursor:pointer;
   color:#ffffff;
   font-family:Arial;
   font-size:17px;
   padding:23px 200px;
   text-decoration:none;
   text-shadow:0px 0px 0px #2f6627;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
   }
   .addStatusButton{
   position:absolute;top:200px;left:100px;
   }
   .addStatusButton:hover {
   background-color:#72cf40;
   }
   .viewStatusButton{
   position:absolute;top:300px;left:100px;
   }
   .viewStatusButton:hover {    
   background-color:#72cf40;
   }
   .viewReportButton{
   position:absolute;top:400px;left:100px;
   }
   .viewReportButton:hover {    
   background-color:#72cf40;
   }
   .logOutButton{
   position:absolute;top:500px;left:100px;
   }
   .logOutButton:hover {    
   background-color:#72cf40;
   }
</style>

my setting.py is as follows:

""" 
Django settings for timesheet_tool project. 

For more information on this file, see 
docs.djangoproject.com/en/1.6/… 

For the full list of settings and their values, see 
docs.djangoproject.com/en/1.6/… 
""" 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
import os 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__)) 

# Quick-start development settings - unsuitable for production 
# See docs.djangoproject.com/en/1.6/… 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '*4j0v-qv3flclm9g-9+s*0+=e&1dj+++5_k(ttalt@#vvxw^v2' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

TEMPLATE_DEBUG = True 

ALLOWED_HOSTS = ['172.17.34.49','indcstools'] 
#TEMPLATE_DIRS = ( 
# os.path.join(SETTINGS_PATH, 'templates'), 
#) 
TEMPLATES = [ 
{ 
'BACKEND': 'django.template.backends.django.DjangoTemplates', 
'DIRS': [os.path.join(SETTINGS_PATH, 'templates'), 
], 
'APP_DIRS': True, 
'OPTIONS': { 
'context_processors': [ 
'django.template.context_processors.request', 
'django.template.context_processors.debug', 
'django.template.context_processors.request', 
'django.contrib.auth.context_processors.auth', 
'django.contrib.messages.context_processors.messages', 
], 
}, 
}, 
] 

# Application definition 

INSTALLED_APPS = ( 
'django_tables2', 
'django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'view_status', 
) 

MIDDLEWARE_CLASSES = ( 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

ROOT_URLCONF = 'timesheet_tool.urls' 

WSGI_APPLICATION = 'timesheet_tool.wsgi.application' 


# Database 
# docs.djangoproject.com/en/1.6/… 

DATABASES = { 
'default': { 
'ENGINE': 'django.db.backends.sqlite3', 
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
} 
} 

# Internationalization 
# docs.djangoproject.com/en/1.6/… 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# docs.djangoproject.com/en/1.6/… 

STATIC_URL = '/static/' 

LOGGING = { 
'version': 1, 
'handlers': { 
'console':{ 
'level':'DEBUG', 
'class':'logging.StreamHandler', 
}, 
}, 
'loggers': { 
'django.request': { 
'handlers':['console'], 
'propagate': True, 
'level':'DEBUG', 
} 
}, 
} 

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

I have also gone through other thread "Page not found 404 Django media files" but it did not help me.

Gahan
  • 4,075
  • 4
  • 24
  • 44
space earth
  • 407
  • 4
  • 18
  • SETTINGS_PATH what it consists? and atleast name the folder properly, use media instead of templates for better understanding – Exprator May 30 '17 at 10:06
  • from where are you trying to check the media files? – Exprator May 30 '17 at 10:10
  • From /welcome_page/, i have included the image/media there.I have seen 404 error code from browser console. – space earth May 30 '17 at 10:12
  • 1
    post the code of the template and the view – Exprator May 30 '17 at 10:13
  • Added in question – space earth May 30 '17 at 10:18
  • so you logo.png and the other is not showing? ok, just tell me the path where logo.png is. the full path – Exprator May 30 '17 at 10:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145438/discussion-between-space-earth-and-exprator). – space earth May 30 '17 at 10:20
  • [Media files](https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-files-uploaded-by-a-user-during-development) are for files uploaded by your users. You shouldn't use `templates` for your `MEDIA_ROOT`. You shouldn't be treating `logo.png` and `sheets.png` as media files. They are [static files](https://docs.djangoproject.com/en/1.9/howto/static-files/). – Alasdair May 30 '17 at 10:23

1 Answers1

2

I have solved my problem by using the static method. I have put the static folder in the view_status directory. So inside /view_status/static/ I have put the logo.png and other images.

Now I have changed the template as follow:

{% load static %} 
<a href="/add_status" class="addStatusButton">Add Status </a>
<a href="/view_status/?page=0&e_date=&s_date=&type=date" class="viewStatusButton">View Status </a>
<a href="#" class="viewReportButton">View Report </a>
<a href="#" class="logOutButton">Logout</a>
<div style="display: flex; justify-content:center;">
   <img src = "{% static 'logo.png' %}" width="150" height="100">
   <img src = "{% static 'Timesheets.png' %}" width="150" height="100">
</div>
<h2><span>DASHBOARD</span></h2>
<p>{{welcome_message}}</p>
<style>
   p    {color: green;
   adding:23px 200px;
   position: absolute;top:130px;left:100px;}
   h2 {
   width: 100%; 
   text-align: center; 
   color:#35a5f0;
   line-height: 1em;
   margin: 10px 0 20px; 
   background-color:#35a5f0;
   } 
   h2 span { 
   background:#fff; 
   padding:0 10px; 
   }
   div.dashBoard {
   color:#35a5f0;
   font-family: 'Tangerine', serif;
   position:absolute;top:300px;left:700px;
   width: 200px; 
   height: 100px; 
   overflow-y: scroll;
   }
   .addStatusButton,.viewStatusButton,.viewReportButton,.logOutButton {
   -moz-box-shadow: 0px 0px 0px 0px #3dc21b;
   -webkit-box-shadow: 0px 0px 0px 0px #3dc21b;
   box-shadow: 0px 0px 0px 0px #3dc21b;
   background-color:#35a5f0;
   -moz-border-radius:35px;
   -webkit-border-radius:35px;
   border-radius:28px;
   border:1px solid #ffffff;
   display:block;
   cursor:pointer;
   color:#ffffff;
   font-family:Arial;
   font-size:17px;
   padding:23px 200px;
   text-decoration:none;
   text-shadow:0px 0px 0px #2f6627;
   font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
   }
   .addStatusButton{
   position:absolute;top:200px;left:100px;
   }
   .addStatusButton:hover {
   background-color:#72cf40;
   }
   .viewStatusButton{
   position:absolute;top:300px;left:100px;
   }
   .viewStatusButton:hover {    
   background-color:#72cf40;
   }
   .viewReportButton{
   position:absolute;top:400px;left:100px;
   }
   .viewReportButton:hover {    
   background-color:#72cf40;
   }
   .logOutButton{
   position:absolute;top:500px;left:100px;
   }
   .logOutButton:hover {    
   background-color:#72cf40;
   }
</style>

In setting.py I have added following:

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

In urls.py I have added following:

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

One thing I have come to know that django will search the static folder from the INSTALLED_APPS.So i have put the static folder in the one of the INSTALLED_APPS that is view_status in my case.

Now it is working fine. Thanks to "Exprator" for his great help and support.

Exprator
  • 26,992
  • 6
  • 47
  • 59
space earth
  • 407
  • 4
  • 18