简体   繁体   English

本地开发时的Django静态文件-如何提供绝对服务?

[英]Django static files when locally developing - how to serve absolutely?

This should be super simple, but somehow its has had me stuck all morning. 这应该是非常简单的,但是不知何故它使我整个上午都陷入困境。 I'm developing locally, using the django debug server, and with this filestructure: 我正在使用django调试服务器在本地开发,并具有以下文件结构:

/project/ (django project)
/static/ (static files)

In the settings.py MEDIA_ROOT and MEDIA_URL are both set to '/static/' and I'm using this in my urls.py 在settings.py中,MEDIA_ROOT和MEDIA_URL都设置为'/ static /',我在urls.py中使用它

url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),

In my templates, the files that need to be server from the static directory are configured as such: 在我的模板中,需要从静态目录作为服务器的文件配置如下:

<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">

That all works as it should - the javascript/css/images are all served properly from the hompage. 所有功能均应正常运作-javascript / css / images均可以从hompage中正常投放。 However, when I go to a subdirectory, such as http://127.0.0.1:8000/news/ then all the links are broken. 但是,当我转到子目录(例如http://127.0.0.1:8000/news/)时,所有链接都断开了。

I've tried using a variety of the os.import options to get it to do the relative links properly, but havent had any luck. 我尝试使用各种os.import选项来使其正确执行相对链接,但是还没有运气。 Is there a way that I could force it to be relative to the base url, or perhaps hardcode it to my filesystem? 有没有一种方法可以强迫它相对于基本URL,或者将其硬编码到我的文件系统中?

Any help would be amazing! 任何帮助都将是惊人的!

I just had the same problem, and I solved it by removing first slash from STATIC_URL='/static/' and making it STATIC_URL = 'static/' just like philipbe wrote above. 我也遇到了同样的问题,我通过从STATIC_URL ='/ static /'中删除第一个斜杠并将其设置为STATIC_URL ='static /'来解决了该问题,就像上面的philipbe一样。 Wierd thing is that '/media/' works fine for MEDIA_URL at the same time (while 'media/' breaks layout). 奇怪的是,'/ media /'同时适用于MEDIA_URL(而'media /'则中断了布局)。

Anyway - just change STATIC_URL to be equal to 'static/' with no leading slash, and you'll solve it. 无论如何-只需将STATIC_URL更改为等于'static /',并且不使用斜杠,就可以解决它。

In this line in your urls.py file, the '../static' should be changed to an absolute directory. 在urls.py文件的这一行中, '../static'应更改为绝对目录。 Try changing it and see what happens. 尝试更改它,看看会发生什么。

Your file: 您的档案:

url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '../static'}),

Should look more like: 应该看起来像:

url(r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/full/path/to/static'}),

To give you an example, mine is set up a little differently, but I still use a full path. 举个例子,我的设置略有不同,但是我仍然使用完整路径。

Here's how mine is setup: in settings.py 这是我的设置方法:在settings.py

STATIC_DOC_ROOT = '/Users/kylewpppd/Projects/Django/kl2011/assets/'

and in urls.py : 并在urls.py

(r'^assets/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes':True}),

I'm serving my static files from 'localhost:8000/assets/' . 我正在从'localhost:8000/assets/'提供我的静态文件。

How do your links break when you go to a subdirectory? 转到子目录时,链接如何断开? Can you explain that further please. 您能否进一步解释一下。

Does django 1.3 support some kind of strange relative url routing for static media? django 1.3是否支持静态媒体某种奇怪的相对URL路由?

If it can be served from the homepage but not others, doesn't that sound exactly like your STATIC_URL is a relative location? 如果可以从首页而不是其他网站上投放广告,那听起来像不是您的STATIC_URL是相对位置吗?

What is your STATIC_URL ? 您的STATIC_URL是什么? It should be absolute and start with a slash. 它应该是绝对的,并以斜杠开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM