Django 1.9.0:找不到页面 404 Django 媒体文件

Posted

技术标签:

【中文标题】Django 1.9.0:找不到页面 404 Django 媒体文件【英文标题】:Django 1.9.0:Page not found 404 Django media files 【发布时间】:2017-10-30 17:15:01 【问题描述】:

我收到 django 媒体文件的错误代码 404。 urs.py如下:

 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 模板

<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_URLlogo.png"  >
   <img src = "MEDIA_URLsheets.png"  >
</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>

我的setting.py如下:

""" 
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')

我也经历过其他线程“Page not found 404 Django media files”,但它没有帮助我。

【问题讨论】:

SETTINGS_PATH 它由什么组成?并至少正确命名文件夹,使用媒体而不是模板以便更好地理解 您尝试从哪里检查媒体文件? 来自 /welcome_page/,我已将图像/媒体包含在其中。我从浏览器控制台看到了 404 错误代码。 贴出模板和视图的代码 添加有问题 【参考方案1】:

我已经通过使用静态方法解决了我的问题。 我已将静态文件夹放在 view_status 目录中。 所以在 /view_status/static/ 里面我放了 logo.png 和其他图片。

现在我已将模板更改如下:

% 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' %"  >
   <img src = "% static 'Timesheets.png' %"  >
</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>

在 setting.py 我添加了以下内容:

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

在 urls.py 我添加了以下内容:

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

我知道 django 会从 INSTALLED_APPS 中搜索静态文件夹。所以我已将静态文件夹放在 INSTALLED_APPS 之一中,在我的例子中是 view_status。

现在它工作正常。 感谢“Exprator”的大力帮助和支持。

【讨论】:

以上是关于Django 1.9.0:找不到页面 404 Django 媒体文件的主要内容,如果未能解决你的问题,请参考以下文章

找不到 Django Python 页面 (404)

如何在 django 中自定义找不到页面(404)?

使用 Django,当我尝试访问应该存在的页面时,为啥找不到 404 错误页面?

在 Django 网站上找不到页面 404?

找不到 Django 404 错误页面...当前路径与最后一个匹配

URL 错误 404 找不到 Django 页面错误 x.html