在Django模型中检测主机域名 [英] detect the HOST domain name in django models
问题描述
在我的模型中,我想使用我在视图中使用的域名(HOST)。
多亏了请求对象,在可行的视图中。但是我该如何用
来建模呢?哪个不使用 HttpRequest对象?
In my model, I want to use the domain name (HOST) I'm using in my views. In views that'd be doable, thanks to the "request" object. But how do I do this models methods? Which don't use "HttpRequest" objects?
现在,我在settings.py中设置了一个全局值HOST并使用了它,但这太丑了
。
Now I'm setting a global value HOST in settings.py and using it, but that's ugly.
此外,我真的不想管理站点(站点应用程序)—有没有办法,
我可以抓住默认网站主机名?
Also, I don't really want to manage "Sites" (the Sites app) — Is there a way, I can grab the "by default" Site Host name?
非常感谢您的帮助! (对不起,我的英语不好)
Thanks a lot for your help! (and sorry for my poor English)
推荐答案
如果您是从视图中调用模型方法,则可以添加一个参数对模型方法的请求,并在从视图中调用它时将其包括在内。例如,
If you're calling the model method from a view, you could add a parameter for the request to the model method and include it when you call it from the view. E.g.
class MyModel(models.Model):
...
def MyMethod(self, request):
# Do whatever with request here
def MyView(request):
mm = MyModel()
mm.MyMethod(request)
这篇关于在Django模型中检测主机域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!