Django: Obtaining the absolute URL without access to a request object

后端 未结 2 480
暖寄归人
暖寄归人 2021-02-07 03:34

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 = mo         


        
2条回答
  •  情深已故
    2021-02-07 04:01

    For getting current site there's object Site:

    If you don’t have access to the request object, you can use the get_current() method of the Site model’s manager. You should then ensure that your settings file does contain the SITE_ID setting. This example is equivalent to the previous one:

    from django.contrib.sites.models import Site
    
    def my_function_without_request():
        current_site = Site.objects.get_current()
        if current_site.domain == 'foo.com':
            # Do something
            pass
        else:
            # Do something else.
            pass
    

    More info: http://docs.djangoproject.com/en/dev/ref/contrib/sites/

提交回复
热议问题