【问题标题】:Absolute URL in html email using Django使用 Django 的 html 电子邮件中的绝对 URL
【发布时间】:2015-01-23 18:07:05
【问题描述】:

使用 Django,我正在发送包含链接的 html 电子邮件,但我无法使链接正常工作。我试过在我的模板中使用{{request.get_full_path}},在我看来也使用request.build_absolute_uri()。任何帮助都会很棒!

在我的网站中,正确的 url 在这样的模板中给出,带有 url="/review_and_export/":

<a href="{url}{{request.get_full_path}}{{ publication.pk }}/?bibtex" target="_blank">BibTex</a>

目前,这是我的电子邮件代码:

views.py

# for links in html email??
full_url = request.build_absolute_uri()
full_url = full_url.split("/software/", 1)[0]   #this produces just the domain, EDIT: i was missing a slash...still having issues

html_content = render_to_string(email_it, {'full_url' : full_url,....}, context_instance=RequestContext(request))

email_it.html

{% for publication in value %}
    {% include "software/display_citations.html" with publication=publication url=full_url%}
{% endfor %}

display_citations.html

# I use full_url and request.get_full_path because get_full_path doesn't return the domain
<a href="{{full_url}}{{request.get_full_path}}{{ publication.pk }}/?bibtex" target="_blank">BibTex</a>

【问题讨论】:

  • 您是否在模板上下文中设置了request? / 为什么不使用full_url 传递给模板?
  • full_url 只生成域(http://localhost:8000/),然后request.get_full_path 获取其余部分(/software/select_citations/review_and_export/email_it/?csrfmiddlewaretoken=wHUsUMnlMiix&amp;email_address=myEmail%40emailAddress.com)。但是,该网址最终与我从网站本身访问链接时得到的不同......
  • 你有django.core.context_processors.request
  • 是的,django.core.context_processors.request 在我的设置文件中...

标签: django django-templates django-urls


【解决方案1】:

我通过在我的视图中进一步操作 url 然后将该结果与我的模板中的参数连接起来来解决这个问题。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2013-01-10
    • 2016-07-20
    • 2015-01-23
    • 1970-01-01
    • 2011-03-15
    相关资源
    最近更新 更多