Django:你如何提供媒体/样式表并在模板中链接到它们 [英] Django: how do you serve media / stylesheets and link to them within templates

查看:18
本文介绍了Django:你如何提供媒体/样式表并在模板中链接到它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问了这个问题的变体,但我仍然无法在呈现模板时正确加载样式表.

Variations of this question have been asked, but I'm still unable to get my stylesheets to load correctly when my templates are rendered.

我正试图在开发过程中从 Django 进程中提供静态媒体——我知道在生产中强烈不鼓励这样做.我会发布我的配置和模板,希望有人能帮助我了解我哪里出错了.

I'm attempting to serve static media from the Django process during development - which is strongly discouraged in production, I'm aware. I'll post my configuration and my template, and hopefully someone can help me to understand where I'm going wrong.

请注意,我确实尝试遵循 Django 项目网站上的示例,但是它没有提到如何从模板中引用您的样式表.我还尝试了同一事物的许多不同变体,因此我的代码/设置可能与所描述的略有不同.

Note that I did try to follow the example on the Django project website, however it doesn't mention how to refer to your stylesheets from a template. I've also tried many different variations of the same thing, so my code/settings may be a little off from what's described.

settings.py

settings.py

MEDIA_ROOT = 'D:/Dev Tools/django_projects/dso/media'
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/media/'

urls.py

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root),
    (r'^ovramt/$', 'dso.ovramt.views.index'),
)

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )

在我的模板中:

<head> 
<title> {% block title %} DSO Template {% endblock %} </title> 
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<link rel="stylesheet" type="text/css" href="../media/styles.css">
</head>

我向您保证,文件(模板/媒体)位于我的文件系统上的正确目录中.如果我需要提供任何额外信息,请发表评论.

I assure you, the files (templates/media) are in the correct directory on my file system. If there's any extra information I need to provide, please post a comment.

我遇到的问题之一是在链接前使用了/".如果前面有正斜杠,则将从站点的根目录打开链接.如果没有正斜杠,则在当前级别打开链接.一个例子:

One of the problems I was having was the use of a '/' prepending my links. If the forward slash is prepended, the link is opened from the root of the site. If there is no forward slash, the link is opened in the current level. An example:

www.example.com/application/有一个链接/app2/"和一个链接app3/".
app2 将在 www.example.com/app2/上打开,app3 将在 www.example.com/application/app3/上打开.我觉得这让我很困惑.

www.example.com/application/ has a link "/app2/ and a link "app3/".
app2 will open at www.example.com/app2/ and app3 will open at www.example.com/application/app3/. This was confusing me I think.

推荐答案

我只能自己解决这个问题.

I just had to figure this out myself.

settings.py:

settings.py:

MEDIA_ROOT = 'C:/Server/Projects/project_name/static/'
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'

urls.py:

from django.conf import settings
...
if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )

模板文件:

<link rel="stylesheet" type="text/css" href="/static/css/style.css" />

文件位于此处:

"C:/Server/Projects/project_name/static/css/style.css"

这篇关于Django:你如何提供媒体/样式表并在模板中链接到它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆