Django:模板路径有问题吗?
经过几个小时的烦躁之后,我终于让我的 django 网站启动并运行了!我现在遇到的唯一问题是所有样式表/图像链接不正确。或者,好吧,它们链接正确,但 django 不会给我这些文件。
这是它的设置方式:
views.py:
from django.shortcuts import render_to_response
def home(request):
return render_to_response('index.html')
urls.py:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
)
并显示index.html,但没有显示其他文件,如图像、样式表等。我该如何解决这个问题?我有一种感觉,这真的很容易!?我尝试谷歌搜索,但找不到任何东西。
提前致谢, 全键盘
So after a few hours of irritation i finally got my django site up and running! The only problem i have now is that all the stylesheets/images are linked incorrectly. Or, well, they are linked correctly but django wont give me the files, kind of.
This is how it's set up:
views.py:
from django.shortcuts import render_to_response
def home(request):
return render_to_response('index.html')
urls.py:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
# Examples:
url(r'^
and that brings up index.html, but none of the other files are shown, like images, stylesheets etc. How do I solve this? I have a feeling it's really easy!? I tried googling, but couldn't find anything.
Thanks in advance,
qwerty
, 'mysite.views.home', name='home'),
)
and that brings up index.html, but none of the other files are shown, like images, stylesheets etc. How do I solve this? I have a feeling it's really easy!? I tried googling, but couldn't find anything.
Thanks in advance,
qwerty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您正在寻找的是能够 提供静态文件。
基本上,您需要在项目中的某个位置添加一个文件夹来保存媒体。然后,您需要编辑 urls.py 和 settings.py 文件以适应对新静态媒体目录的访问。
urls.py
settings.py
然后在你的模板中你可以这样做:
或者任何你想要的。这适用于 javascript、css 和图像文件。
It sounds like what you're looking for is the ability to serve static files.
Basically, you'll need to add a folder somewhere in your project to save the media to. Then, you'll need to edit your urls.py and settings.py files to accommodate access to your new static media directory.
urls.py
settings.py
Then in your template you can do this:
Or whatever you want. This will work for javascript, css and image files.
扩展 Shamanu4 的评论:您要求的是如何提供静态文件。出于开发目的,您可以使用静态文件服务器。
但从长远来看,这并不是一个最佳解决方案。最简单的方法是隔离所有静态文件,并通过不同的路径直接通过 Web 浏览器提供它们。在 Apache 中,如果您首先配置静态路径,则此静态文件路径可以位于您的 Django 路径内。
不过,如果您需要高性能,Django 团队建议您使用轻量级、速度优化的服务器(例如 lighttpd)来提供静态文件,并使用另一台支持 WSGI 的服务器(例如 Apache)来为 Django 提供服务。
在我工作的 Django 项目中,我从
/djangoprojname/
提供 Django 服务,并从/djangoprojname/static/
提供静态文件。在磁盘上,static
目录与我的 Django 项目目录处于同一级别。两者都位于 Mercurial 存储库中。在static/
中,我有css/
、js/
和img/
,在这些目录中,我每个应用程序有一个目录,名称与应用程序相同。这可以防止事情变得混乱。我的
django.conf
(在 Fedora 或 RHEL 上的/etc/httpd/conf.d
中)看起来像:对于开发,我将其添加到项目 < 的末尾code>urls.py:
如果在开发中运行,则在我的
settings.py
中将settings.IS_DEV
设置为True
服务器。如果使用runserver
,则修改manage.py
以设置环境变量,并且settings.py
检查此变量。MEDIA_ROOT
设置为static
目录的路径。Expanding on Shamanu4's comment: what you're asking for is how static files are served. For development purposes, you can use the static file server.
Long term, though, this is not an optimal solution. The easy way is to segregate all your static files and serve them directly through your web browser via a different path. In Apache, this static file path can be inside your Django path if you configure the static path first.
If you need high performance, though, the Django team recommends that you use a lightweight, speed-optimized server (such as lighttpd) to serve static files and another server with WSGI support (such as Apache) to serve Django.
In the Django project I have at work, I have Django served from
/djangoprojname/
and static files served from/djangoprojname/static/
. On disk, thestatic
directory is at the same level as my Django project's directory. both of which are in a Mercurial repository. Withinstatic/
, I havecss/
,js/
, andimg/
, and within those directories, I have one directory per app, named the same as the app. This keeps things from getting messy.My
django.conf
(in/etc/httpd/conf.d
on Fedora or RHEL) looks something like:For development, I added this to the end of my project's
urls.py
:settings.IS_DEV
is set in mysettings.py
toTrue
if this is running on a development server.manage.py
is modified to set an environment variable ifrunserver
is used, andsettings.py
checks for this variable.MEDIA_ROOT
is set to the path to thestatic
directory.您的 settings.py 中的 MEDIA_ROOT 和 MEDIA_URL 可能存在问题。请参考 http://docs.djangoproject.com/en/1.3 /ref/settings/#media-root
You are probably having problem with MEDIA_ROOT and MEDIA_URL in your settings.py. Please refer to http://docs.djangoproject.com/en/1.3/ref/settings/#media-root