Django 设置变量在传递给模板时丢失
我有一个奇怪的问题。 基本上,在我的settings.py文件中,我有4个变量
URL_MAIN = 'http://www.mysite'
URL_JOBS = 'http://jobs.mysite'
URL_CARS = 'http://cars.mysite'
URL_HOMES = 'http://homes.mysite'
在我的views.py中,我有通常的变量:
from settings import *
我有6个视图调用它们,然后将它们返回到上下文中的模板:
class CarsHp(TemplateView):
...
class JobsHp(TemplateView):
...
class HomesHp(TemplateView):
...
class CarsList(TemplateView):
...
class JobsList(TemplateView):
...
class HomesList(TemplateView):
...
这些变量在url中被调用
CarsList.as_view()
...
所有这些视图都有同样的语句:
context['URL_MAIN'] = URL_MAIN
...
对于所有 4 个变量。
在模板中,我正确地获取了所有 4 个视图,但 URL_MAIN 除外,它在这 6 个视图中的 2 个中“丢失”了。我正在使用经典的 {{ URL_MAIN }} 访问它们,并且我一直在尝试一切,从移动到重命名,但在从 2 提供服务后, URL_MAIN 仍然没有显示(我得到空字符串,没有排序错误)这些观点。所有功能基本上共享相同的代码(查询和数据处理部分除外),并且这些设置的变量只是被分配和返回。没有任何形式的检查或修改。我一直在尝试使用 django 的 shell,并且我总是可以检索它们。
我们由 apache 提供服务,并为 robots.txt 文件和静态文件提供一些代理传递配置。不严重”。
我不会发布所有 6 个视图的源代码,只是因为它们很长并且相关部分都在上面进行了描述。但如果你愿意的话,我可以发布它们,我只是不知道它是否真的有用,因为我已经三次检查所有来源是否存在名称或双重声明或不正确使用的冲突。
预先感谢大家,这真的让我的大脑震惊
i have a weird problem.
Basically, in my settings.py file i have 4 variables
URL_MAIN = 'http://www.mysite'
URL_JOBS = 'http://jobs.mysite'
URL_CARS = 'http://cars.mysite'
URL_HOMES = 'http://homes.mysite'
In my views.py i have the usual:
from settings import *
I have 6 views calling them and just returning them to templates inside the context:
class CarsHp(TemplateView):
...
class JobsHp(TemplateView):
...
class HomesHp(TemplateView):
...
class CarsList(TemplateView):
...
class JobsList(TemplateView):
...
class HomesList(TemplateView):
...
which are being called in urls by
CarsList.as_view()
...
All of those views have the same statement:
context['URL_MAIN'] = URL_MAIN
...
for all 4 variables.
In templates i'm correctly getting all 4 of them, except for URL_MAIN, which "gets lost" in 2 of those 6 views. I'm accessing them with classical {{ URL_MAIN }} and i've been trying everything, from moving to renaming, but still that URL_MAIN doesn't show up (i get empty string, no errors of sort) after being served from 2 of those views. All the functions basically share the same code (except for the querying and data processing part) and those settings' variables are just being assigned and returned off. Not any sort of check nor modification. I've been trying with django's shell, and i could always retrieve them.
We're being served by apache, with some proxypassing configurations for the robots.txt file and static files. Nothing "serious".
I'm not posting all the 6 views source codes just because they're long and the relevant parts are all described above. But i can post them if you want,i just don't know if it is actually useful since i've been triple checking all the sources for clashing on names or double declarations or incorrect use.
Thanks all in advance, this is really stunning my brain
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,您应该为此使用模板上下文处理器。它将减少您的代码并让您准确地看到问题出在哪里。
在您的项目中创建一个名为 urls_context_processor.py (或类似)的文件,并将变量放入其中:
在您的 settings.py 中,
现在 urls 变量将在您的所有模板中自动可用,您将获胜不需要将它们硬编码到每个视图中。
Ideally, you should use template context processors for this. It will cut down your code and allow you to see exactly where the problem is.
Make a file in your projects called urls_context_processor.py (or similar) and put your variables in there:
and in your settings.py
now the urls variables will be automatically available in all your template, and you won't need to hard code them into every view.