简体   繁体   English

如何在使用django manage.py runserver进行开发时使用远程静态文件服务器

[英]How to use a remote static file server while developing with django manage.py runserver

Background: I'm using the Django manage.py runserver for local development. 背景:我正在使用Django manage.py runserver进行本地开发。 I have the contrib.staticfiles in installed apps and I'm using its {% static %} template tag in my templates. 我在已安装的应用程序中有contrib.staticfiles,我在我的模板中使用其{%static%}模板标记。

What I try to achieve: For development, I'd like to use an independent server for serving the static files but use the django development server to serve the Django app. 我试图实现的目标:对于开发,我想使用独立的服务器来提供静态文件,但是使用django开发服务器来为Django应用程序提供服务。 So that I could access the page locally on my computer at http://127.0.0.1:8000 , but all the static files would be served from another computer or from a different server on the localhost, as defined by the settings.STATIC_URL variable. 因此,我可以在我的计算机本地访问该页面, 网址http://127.0.0.1:8000 ,但所有静态文件将从另一台计算机或本地主机上的其他服务器提供,由settings.STATIC_URL变量定义。

The problem: The settings.STATIC_URL variable is somehow overridden when I'm using the development server. 问题:当我使用开发服务器时,settings.STATIC_URL变量以某种方式被覆盖。 So all my static files are served by the local django development server instead of what I defined in settings.STATIC_URL. 所以我的所有静态文件都由本地django开发服务器提供,而不是我在settings.STATIC_URL中定义的。

Solution: See Daniel's answer below! 解决方案:请参阅下面的丹尼尔答案!

settings.py: settings.py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = os.path.join('127.0.0.1:666', BASE_DIR, 'static/')

example_template.html: example_template.html:

{% load staticfiles %}
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery=True%}
{% bootstrap_messages %}

{# Load MyApp CSS #}
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700subset=greek-ext,vietnamese,cyrillic-ext,latin-ext' rel='stylesheet' type='text/css'>
<link href="{% static 'css/my_app.main.css' %}" rel="stylesheet">

page source when using a browser: 使用浏览器时的页面源:

<link href="FULL_PATH_TO_BASE_DIR_HERE/static/css/my_app.main.css" rel="stylesheet">

but expected to see: 但预计会看到:

<link href="127.0.0.1:666/FULL_PATH_TO_BASE_DIR_HERE/static/css/my_app.main.css" rel="stylesheet">

This isn't anything to do with Django or runserver. 这与Django或runserver无关。 It's simply because you are using os.path.join() to join a domain and a path; 这只是因为你使用os.path.join()来加入域和路径; that function doesn't know about URLs, and will assume that since BASE_DIR starts with a leading slash, it should normalize the whole path to start from there and ignore anything previous: 该函数不知道URL,并假设由于BASE_DIR以前导斜杠开头,它应该规范化整个路径从那里开始并忽略之前的任何内容:

>>> os.path.join('127.0.0.1', '/foo', 'static')
'/foo/static'

The solution is twofold: don't use os.path.join on URLs, but more importantly, don't use BASE_DIR in your STATIC_URL. 解决方案有两个:不要在URL上使用os.path.join ,但更重要的是,不要在STATIC_URL中使用BASE_DIR。 The filesystem directory your static files live in has nothing whatsoever to do with the URL they are exposed on. 静态文件所在的文件系统目录与它们所暴露的URL没有任何关系。 Your STATIC_URL should be something like " http://127.0.0.1:666/static/ ". 你的STATIC_URL应该是“ http://127.0.0.1:666/static/ ”。

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

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