Django:获取绝对URL,无需访问请求对象 [英] Django: Obtaining the absolute URL without access to a request object

查看:406
本文介绍了Django:获取绝对URL,无需访问请求对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面的模型。当创建一个实例时,我想发送一封电子邮件给感兴趣的一方:

  class TrainStop(models.Model )
name = models.CharField(max_length = 32)
notify_email = models.EmailField(null = True,blank = True)

def new_stop_created(发件人,实例,创建,* args,** kwargs):

#仅限新站点
如果未创建或instance.id为无:return

#发送状态链接
如果instance.notify_email:
send_mail(
subject ='停止提交:%s'%instance.name,
message ='检查状态:%s'%reverse stop_status',kwargs = {'status_id':str(instance.id),}),
from_email='admin@example.com',
recipient_list = [instance.notify_email,]

signals.post_save.connect(new_stop_created,sender = TrainStop)

但是, code> reverse call只返回路径portio n的URL。示例: / stops / 9 / status / 。我需要一个完整的网址,如 http://example.com/stops/9/status/ 。我如何检索当前网站的主机名和端口(对于不使用端口80的测试实例)?



我最初的想法是通过在 settings.py 中的变量,我可以根据需要访问。但是,以为有人可能会有更强大的建议。

解决方案

有网站框架,如yedpodtrzitko所提到的, ,这是一个手动设置。



在settings.py中需要一个设置,但它比设置网站只少一些手动。 (它可以处理多个域,以及站点和 SITE_ID 设置可以)。



有一个想法对于替换get_absolute_url ,这将使这样的东西更容易,尽管我认为其实现受到相同的影响问题(如何获取域,方案[http vs https]等)。



我一直在想一个检查传入请求和构造的中间件的想法基于HTTP HOST头的值的频率,某种最可能的域设置。或者也可以单独对每个请求设置此设置,因此您可以随时使当前域使用。我没有认真看待它,但这是一个想法。


I have a model like the one below. When an instance is created, I want to send out an e-mail to an interested party:

class TrainStop(models.Model):
    name = models.CharField(max_length=32)
    notify_email = models.EmailField(null=True, blank=True)

def new_stop_created(sender, instance, created, *args, **kwargs):

    # Only for new stops
    if not created or instance.id is None: return

    # Send the status link
    if instance.notify_email:
        send_mail(
            subject='Stop submitted: %s' % instance.name,
            message='Check status: %s' % reverse('stop_status', kwargs={'status_id':str(instance.id),}),
            from_email='admin@example.com',
            recipient_list=[instance.notify_email,]
        )
signals.post_save.connect(new_stop_created, sender=TrainStop)

However, the reverse call only returns the path portion of the URL. Example: /stops/9/status/. I need a complete URL like http://example.com/stops/9/status/. How would I go about retrieving the hostname and port (for test instances that do not use port 80) of the current website?

My initial thought was to make this available via a variable in settings.py that I could then access as needed. However, thought someone might have a more robust suggestion.

解决方案

There's the sites framework, as yedpodtrzitko mentioned, but, as you mentioned, it's very much a manual setup.

There's requiring a setting in settings.py, but it's only slightly less manual than setting up sites. (It can handle multiple domains, just as well as sites and the SITE_ID setting can).

There's an idea for replacing get_absolute_url, that would make stuff like this easier, though I think its implementation suffers from the same problem (how to get the domain, scheme [http vs https], etc).

I've been toying with the idea of a middleware that examines incoming requests and constructs a "most likely domain" setting of some sort based on the frequency of the HTTP HOST header's value. Or perhaps it could set this setting on each request individually, so you could always have the current domain to work with. I haven't gotten to the point of seriously looking into it, but it's a thought.

这篇关于Django:获取绝对URL,无需访问请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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