Django STATIC_URL 值在生产中无法识别
我正在尝试使用 Apache+mod_wsgi 将我的 Django 应用程序部署到生产环境中,但遇到了静态文件的麻烦。由于某种原因,我在生产中的模板无法识别 {{ STATIC_URL }}
。在开发中,我的静态文件的 URL 为:
<script type="text/javascript" src="/static/js/myfile.js"></script>
但在生产中,URL 为:
<script type="text/javascript" src="js/myfile.js"></script>
当我尝试打印 {{STATIC_URL}}
时,它在生产中为空白。我在这里缺少什么?
谢谢!
I'm trying to deploy my Django app onto production with Apache+mod_wsgi, but running into troubles withs static files. For some reason {{ STATIC_URL }}
is not being recognized by my templates in production. In development my static files have the URL:
<script type="text/javascript" src="/static/js/myfile.js"></script>
but in production the URL is:
<script type="text/javascript" src="js/myfile.js"></script>
When I try to print out {{STATIC_URL}}
it is blank in production. What am I missing here?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚遇到这个问题,试图自己解决这个问题。对我来说关键的一步是我的视图传递的是
Context
而不是RequestContext
对象。为了解决这个问题,在我使用 render_to_response 方法的地方,我必须提供以下第三个参数:您的问题显然已经解决,但这只是为了防止其他人遇到同样的问题。
Just came across this trying to solve this problem myself. The key step for me was that my views were being passed a
Context
rather than aRequestContext
object. To solve this, where I was using therender_to_response
method, I had to provide the following third parameter:Your problem was obviously solved, but this is just in case someone else runs into the same issue.
mod_wsgi 使用与您的 shell 相同的 python 吗?和相同的 PYTHONPATH 吗?
我建议尝试另一个网络服务器,只是为了知道这是 mod_wsgi 问题还是您的设置/环境问题。 Gunicorn 的设置非常简单。要使用它:
pip install Gunicorn
gunicorn
添加到您的INSTALLED_APPS中,./manage.py run_gunicorn
运行生产服务器Is mod_wsgi using the same python as your shell? And the same PYTHONPATH?
I suggest to try another web server, just to know if it's a mod_wsgi problem or it's a problem with your setup/environment. Gunicorn is very easy to set up. To use it:
pip install gunicorn
gunicorn
to your INSTALLED_APPS./manage.py run_gunicorn