在 Django 1.3 media_url 与 static_url 中显示样式
Displaying styles in Django 1.3 media_url vs. static_url
我在我的实时网站 (Django 1.3) 上设置了样式保存的所有内容。下面是 settings.py 的非样板代码:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = "/home2/django/build/projects/static/media/"
MEDIA_URL = "/media/"
STATIC_ROOT = "home2/django/build/projects/static/"
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
这是 urls.py 的样子:
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'seeking_alpha.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/', 'seeking_alpha.views.about', name = 'about'),
url(r'^guidelines/', 'seeking_alpha.views.guidelines', name = 'guidelines'),
url(r'^participation/', 'seeking_alpha.views.participation', name = 'participation'),
url(r'^evaluation/', 'seeking_alpha.views.evaluation', name = 'evaluation'),
url(r'^home/', 'seeking_alpha.views.home', name = 'home')
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
和 views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
def home(request):
c = {}
return render(request, "home.html", c)
def about(request):
c = {}
return render(request, "about.html", c)
def guidelines(request):
c = {}
return render(request, "guidelines.html", c)
def participation(request):
c = {}
return render(request, "participation.html", c)
def evaluation(request):
c = {}
return render(request, "evaluation.html", c)
最后是模板中的一些代码:
{% load static %}
<DOCTYPE! html>
<html>
<head>
<title>2015 UChicago Seeking Alpha Pitch Competition</title>
<link rel = "stylesheet" type = "style/css" href = "{% get_static_prefix %} participation.css">
</head>
<body>
<div class = "container">
<div class = "header">
<img id = "alpha" src = "{% get_static_prefix %} alpha.jpg" height = "60", width = "250">
用 Django 编写,由 Hostgator 托管。所有 ht 逻辑都工作正常(链接、内容),css 页面或图像中只有 none 正在通过用户看到的内容
尝试在 STATIC_ROOT
路径的开头插入 /
settings.py
。它应该有所帮助,因为现在它是一条相对路径,你需要一个绝对路径。它应该是这样的:
STATIC_ROOT = "/home2/django/build/projects/static/"
我在我的实时网站 (Django 1.3) 上设置了样式保存的所有内容。下面是 settings.py 的非样板代码:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = "/home2/django/build/projects/static/media/"
MEDIA_URL = "/media/"
STATIC_ROOT = "home2/django/build/projects/static/"
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
这是 urls.py 的样子:
from django.conf.urls.defaults import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'seeking_alpha.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^about/', 'seeking_alpha.views.about', name = 'about'),
url(r'^guidelines/', 'seeking_alpha.views.guidelines', name = 'guidelines'),
url(r'^participation/', 'seeking_alpha.views.participation', name = 'participation'),
url(r'^evaluation/', 'seeking_alpha.views.evaluation', name = 'evaluation'),
url(r'^home/', 'seeking_alpha.views.home', name = 'home')
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
和 views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
def home(request):
c = {}
return render(request, "home.html", c)
def about(request):
c = {}
return render(request, "about.html", c)
def guidelines(request):
c = {}
return render(request, "guidelines.html", c)
def participation(request):
c = {}
return render(request, "participation.html", c)
def evaluation(request):
c = {}
return render(request, "evaluation.html", c)
最后是模板中的一些代码:
{% load static %}
<DOCTYPE! html>
<html>
<head>
<title>2015 UChicago Seeking Alpha Pitch Competition</title>
<link rel = "stylesheet" type = "style/css" href = "{% get_static_prefix %} participation.css">
</head>
<body>
<div class = "container">
<div class = "header">
<img id = "alpha" src = "{% get_static_prefix %} alpha.jpg" height = "60", width = "250">
用 Django 编写,由 Hostgator 托管。所有 ht 逻辑都工作正常(链接、内容),css 页面或图像中只有 none 正在通过用户看到的内容
尝试在 STATIC_ROOT
路径的开头插入 /
settings.py
。它应该有所帮助,因为现在它是一条相对路径,你需要一个绝对路径。它应该是这样的:
STATIC_ROOT = "/home2/django/build/projects/static/"