使用django manage.py runserver开发时如何使用远程静态文件服务器 [英] How to use a remote static file server while developing with django manage.py runserver

查看:56
本文介绍了使用django manage.py runserver开发时如何使用远程静态文件服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用Django manage.py runserver进行本地开发.我已安装的应用程序中有contrib.staticfiles,并且我在模板中使用其{%static%}模板标记.

Background: I'm using the Django manage.py runserver for local development. I have the contrib.staticfiles in installed apps and I'm using its {% static %} template tag in my templates.

我想要实现的目标:对于开发,我想使用一个独立的服务器来提供静态文件,但是要使用django开发服务器来提供Django应用.这样我就可以在计算机上的 http://127.0.0.1:8000 上本地访问该页面,但所有静态文件将由settings.STATIC_URL变量定义的另一台计算机或本地主机上的其他服务器提供.

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. 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.

问题:当我使用开发服务器时,settings.STATIC_URL变量会以某种方式被覆盖.因此,我所有的静态文件都是由本地django开发服务器提供的,而不是我在settings.STATIC_URL中定义的文件.

The problem: The settings.STATIC_URL variable is somehow overridden when I'm using the development server. So all my static files are served by the local django development server instead of what I defined in 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">

但希望看到:

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

推荐答案

与Django或运行服务器无关.这仅仅是因为您正在使用 os.path.join()来连接域和路径;该函数不知道URL,并且会假设由于BASE_DIR以一个斜杠开头,因此它应规范化从此处开始的整个路径,并忽略之前的任何内容:

This isn't anything to do with Django or runserver. It's simply because you are using os.path.join() to join a domain and a path; 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:

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

解决方案是双重的:不要在URL上使用 os.path.join ,但更重要的是,请不要在STATIC_URL中使用BASE_DIR.静态文件所在的文件系统目录与它们公开的URL无关.您的STATIC_URL应该类似于" http://127.0.0.1:666/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. The filesystem directory your static files live in has nothing whatsoever to do with the URL they are exposed on. Your STATIC_URL should be something like "http://127.0.0.1:666/static/".

这篇关于使用django manage.py runserver开发时如何使用远程静态文件服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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