Django STATIC_URL 值在生产中无法识别

发布于 2024-11-03 07:39:21 字数 438 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

伴梦长久 2024-11-10 07:39:21

刚刚遇到这个问题,试图自己解决这个问题。对我来说关键的一步是我的视图传递的是 Context 而不是 RequestContext 对象。为了解决这个问题,在我使用 render_to_response 方法的地方,我必须提供以下第三个参数:

context_instance=RequestContext(request)

您的问题显然已经解决,但这只是为了防止其他人遇到同样的问题。

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 a RequestContext object. To solve this, where I was using the render_to_response method, I had to provide the following third parameter:

context_instance=RequestContext(request)

Your problem was obviously solved, but this is just in case someone else runs into the same issue.

伴我心暖 2024-11-10 07:39:21

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
  • add gunicorn to your INSTALLED_APPS
  • run the production server with ./manage.py run_gunicorn
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文