我收到了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_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>
我的 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”,但它对我没有帮助。
答案 0 :(得分:2)
我通过使用静态方法解决了我的问题。 我已将静态文件夹放在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' %}" 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>
在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。
现在工作正常。 感谢&#34; Exprator&#34;得到他的大力帮助和支持。