Django:模板路径有问题吗?

发布于 2024-11-09 08:36:36 字数 577 浏览 0 评论 0原文

经过几个小时的烦躁之后,我终于让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£噩梦荏苒 2024-11-16 08:36:36

听起来您正在寻找的是能够 提供静态文件

基本上,您需要在项目中的某个位置添加一个文件夹来保存媒体。然后,您需要编辑 urls.py 和 settings.py 文件以适应对新静态媒体目录的访问。

urls.py

urlpatterns = patterns('',
    # Examples:
    url(r'^

settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# Should be the location where you put your static folder.
# Should be different for testing and production environments.
STATIC_ROOT =  os.path.join(PROJECT_ROOT, 'media')

# This is assuming that your settings.py file is in path/to/project/ and your
# static files are in path/to/project/media/

然后在你的模板中你可以这样做:

< img src="/static/my_image.jpg" / >

或者任何你想要的。这适用于 javascript、css 和图像文件。

, 'mysite.views.home', name='home'), (r'^static/(?P<path>.*)

settings.py


然后在你的模板中你可以这样做:


或者任何你想要的。这适用于 javascript、css 和图像文件。

, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), )

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

urlpatterns = patterns('',
    # Examples:
    url(r'^

settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# Should be the location where you put your static folder.
# Should be different for testing and production environments.
STATIC_ROOT =  os.path.join(PROJECT_ROOT, 'media')

# This is assuming that your settings.py file is in path/to/project/ and your
# static files are in path/to/project/media/

Then in your template you can do this:

< img src="/static/my_image.jpg" / >

Or whatever you want. This will work for javascript, css and image files.

, 'mysite.views.home', name='home'), (r'^static/(?P<path>.*)

settings.py


Then in your template you can do this:


Or whatever you want. This will work for javascript, css and image files.

, 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), )

settings.py

Then in your template you can do this:

Or whatever you want. This will work for javascript, css and image files.

暮色兮凉城 2024-11-16 08:36:36

扩展 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 中)看起来像:

WSGIDaemonProcess djangoprojname threads=15
WSGISocketPrefix /var/run/wsgi/wsgi

Alias /djangoprojname/static/ /var/www/djangoprojname/static/
Alias /djangoprojname/admin/media/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
WSGIScriptAlias /djangoprojname /var/www/djangoprojname/django.wsgi
WSGIProcessGroup djangoprojname

<Directory /var/www/djangoprojname>
    Order deny,allow
    Allow from all
</Directory>

<Directory /usr/lib/python2.6/site-packages/django/contrib/admin/media>
    Order deny,allow
    Allow from all
</Directory>

对于开发,我将其添加到项目 < 的末尾code>urls.py:

# Only serve static media if in development (runserver) mode.
if settings.IS_DEV:
    urlpatterns += patterns('',
        url(r'^static/(?P<path>.*)

如果在开发中运行,则在我的 settings.py 中将 settings.IS_DEV 设置为 True服务器。如果使用 runserver,则修改 manage.py 以设置环境变量,并且 settings.py 检查此变量。 MEDIA_ROOT 设置为 static 目录的路径。

, 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), )

如果在开发中运行,则在我的 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, the static directory is at the same level as my Django project's directory. both of which are in a Mercurial repository. Within static/, I have css/, js/, and img/, 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:

WSGIDaemonProcess djangoprojname threads=15
WSGISocketPrefix /var/run/wsgi/wsgi

Alias /djangoprojname/static/ /var/www/djangoprojname/static/
Alias /djangoprojname/admin/media/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
WSGIScriptAlias /djangoprojname /var/www/djangoprojname/django.wsgi
WSGIProcessGroup djangoprojname

<Directory /var/www/djangoprojname>
    Order deny,allow
    Allow from all
</Directory>

<Directory /usr/lib/python2.6/site-packages/django/contrib/admin/media>
    Order deny,allow
    Allow from all
</Directory>

For development, I added this to the end of my project's urls.py:

# Only serve static media if in development (runserver) mode.
if settings.IS_DEV:
    urlpatterns += patterns('',
        url(r'^static/(?P<path>.*)

settings.IS_DEV is set in my settings.py to True if this is running on a development server. manage.py is modified to set an environment variable if runserver is used, and settings.py checks for this variable. MEDIA_ROOT is set to the path to the static directory.

, 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), )

settings.IS_DEV is set in my settings.py to True if this is running on a development server. manage.py is modified to set an environment variable if runserver is used, and settings.py checks for this variable. MEDIA_ROOT is set to the path to the static directory.

Spring初心 2024-11-16 08:36:36

您的 settings.py 中的 MEDIA_ROOTMEDIA_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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文