Django:使用<a> element in template

时间:2018-11-21 13:29:10

标签: python django

I'm wondering how I can change in Django this link :

<a href="http://localhost:8000{% url 'my-token' token=token %}">{{title}}</a>

I would like to set http://localhost:8000 dynamically. If I'm working in local, it will be http://localhost:8000 and if I am on my dev server or production server it could be https://subdomaine.domain.com

My idea :

I could create different settings file : local.py / dev.py / prod.py and define inside each one :

#local.py
SITE_URL = "http://localhost:8000"
#dev.py
SITE_URL = "http://dev.domain.com"
#prod.py
SITE_URL = "http://prod.domain.com"

So how I can handle my <a> link to add SITE_URL ?

Maybe with something like that {{ request.META.HTTP_HOST }} ?

SOLUTION :

You can find here the view which generate my email with Django CBV :

class HomeView(TemplateView):
    def send_email(self, email, upload, title, token):
        context = {
            'document_link': upload,
            'publication_title': title,
            'token': token,
            'MY_SITE_URL': settings.MY_SITE_URL
        }
        subject = 'my subject'
        message = get_template('emails/message.txt').render(context)
        mail = EmailMultiAlternatives(subject, message, 'toto@test.eu', [email])
        html_message = get_template('emails/message.html').render(context)
        mail.attach_alternative(html_message, "text/html")
        mail.send(fail_silently=False)

And the message.html file :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>

<p>== This E-mail is automatically generated, please do not reply to it ==</p>

<br>

<p>Please find below your download link for the free publication:</p>

<br>

<a href="{{ MY_SITE_URL }}{% url 'freepub-token' token=token %}">{{ publication_title }}</a>

<br>

<p>You can use this link as much as you like. It will expire in 10 minutes.</p>

<br>

<p>Thank you for your interest to publications.</p>

</body>
</html>

4 个答案:

答案 0 :(得分:1)

您可以添加一个自定义的context_processor:

def site(request):
    return {'SITE_URL': settings.SITE_URL}

然后将其添加到您的CONTEXT_PROCESSOR设置

 mysite.context_processors.site,

和标签{{SITE_URL}}将可用。

答案 1 :(得分:1)

您可以使用上下文处理器(link)来解决此问题-通过编写一个自定义的变量来进行设置。SITE_URL在您渲染的每个模板中均可用。

或者,如果request对象具有所需的数据,则可以启用并使用内置的django.template.context_processors.request上下文处理器(link

答案 2 :(得分:1)

好吧,您可以使用reverserequest.build_absolute_uri来解决此问题。例如:

from django.core.mail import EmailMultiAlternatives


def send_email(request):
    subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
    text_content = 'This is an important message.'
    url_val = request.build_absolute_uri(reverse('my-token', args=['token']))
    html_content = '<p>This is an <strong>important</strong> message.</p>. <a>{}</a>'.format(url_val)  # or pass url_val as context to email template
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
    msg.attach_alternative(html_content, "text/html")
    msg.send()

更新

怎么样

<a href="{{ request.get_host }}{% url 'my-token' token=token %}"

答案 3 :(得分:0)

说明: 您可以在每个设置文件(local.py,dev.py,prod.py)中分别设置ALLOWED_HOSTS变量。然后,您可以按照以下说明将主机名放入模板上下文中进行渲染。

原始答案:

我正在context processor中做这件事:

hostname = request.META.get('SERVER_NAME')

,然后从上下文处理器返回主机名。

更多点:您可以了解有关request.META的更多信息,包括如何获取端口号here,以及是否需要确定端口号是http还是https,只需使用{{3 }}。