- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下 HTML 文件 template.html:
{% load i18n %}
<p>{% blocktrans %}You have following email: {{ request.user.email }}.{% endblocktrans %}</p>
现在在 python 中:
if request.user is not None and request.user.is_authenticated():
text = render_to_string('template.html', context_instance=RequestContext(request)))
但是request.user.email 在模板中是空的。即使我写了 {{ user.email }}
,它仍然是空的。如何呈现用户并正确调用其方法?例如,{{ request.user.get_short_name }}
也不起作用。
更新:问题出在 {% blocktrans %}
<p>You have following email: {{ request.user.email }}.</p>
谁能告诉我为什么?我还没有翻译消息,但我认为它会按原样呈现。
最佳答案
如文档所述,您不能在 {% blocktrans %}
中直接使用模板表达式,只能使用变量:
To translate a template expression – say, accessing object attributes or using template filters – you need to bind the expression to a local variable for use within the translation block. Examples:
{% blocktrans with amount=article.price %}
That will cost $ {{ amount }}.
{% endblocktrans %}
比照https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#blocktrans-template-tag
所以你的模板代码应该是这样的:
{% load i18n %}
<p>
{% blocktrans with email=request.user.email %}
You have following email: {{ email }}.
{% endblocktrans %}
</p>
关于python - Django render_to_string 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888621/
我在 Django 中创建了一个不像按钮,但我撞到了墙上。我正在使用 ajax 抓取 id 并发布以呈现为字符串。第一次单击按钮时一切正常,但 render_to_string 不返回值 attr。我
为什么我的电子邮件(正文部分)不包含该消息? 这是我的代码: message = render_to_string('contact_template.txt', {'contact_name':
我在使用 rails render_to_string 函数时遇到了困难。我已经使用 --api 标志创建了一个应用程序,所以我认为这可能是问题,因为我已经在“完整”rails 应用程序中进行了测试,
我在这里遗漏了一 block ,希望有人能指出我做错了什么。 Mojolicious 应用程序有一个路由/export,它创建一个 href 并将该数据发送到 export.html.ep 模板以呈现
我正在尝试在 Django 中执行单元测试。我在 index.html 中有以下表格: {% csrf_token %} 我正在测试 View 是否正确呈现模板: View .py de
尝试执行以下操作 @message = render_to_string ( :sender => sender, :template => "template" ) 但是当在模板中
我有以下 HTML 文件 template.html: {% load i18n %} {% blocktrans %}You have following email: {{ request.use
我遇到了 render_to_string 的问题使用 Rails 3.2.18,其中函数试图呈现错误的 View 格式,尽管其 :formats选项设置正确(例如,当 kml 设置为 :format
我使用的是 Django 1.9。 使用render_to_string,我可以轻松地将渲染的 html 作为 json 传递到我的客户端脚本。但是,由于模板依赖user等变量,所以我还需要传递con
目前我引用了我公司的 django 1.9 版本中创建的一些旧项目,我发现他们已经多次使用 render_to_string 函数,现在我正在使用渲染函数,但我从未使用过 render_to_stri
我需要使用render_to_string,但是我不能使用 @csrf_protect 并通过 context_instance=RequestContext(request)) 到 render_t
我的 Rails 应用程序从 Rails 6 升级到 Rails 7 后遇到问题。当我尝试 render_to_string 模板时,我收到 ActionView::MissingTemplate 异
我尝试使用 gem wicked_pdf 将 Rails View 转换为 pdf。但是当我像这样做一个 render_to_string 时 ActionController::Base.new.r
我想使用 Rake 任务来缓存我的站点地图,以便对 sitemap.xml 的请求不会永远。这是我到目前为止所拥有的: @posts = Post.all sitemap = render_t
我有一个需要将 View 呈现为字符串的操作。该 View 称为 index.xml.erb。我正在尝试使用 render_to_string 来实现这一点: my_string = render_t
我正在尝试使用 delayed_job通过 xml 更新远程数据库 在我的 lib 文件夹中,我放了一个文件,该文件的类应该执行 render_to_text与 template.xml.builde
对于其中一个应用程序,我在使用管理面板的 Django 1.9.x 项目中重载了“删除所选对象”方法。为此,我有一个与此类似的代码: from django.contrib.admin import
当用户成功更新密码时,我正在为他们起草一个电子邮件模板。 我在模板中使用了 {{ autoescape off }} ,它是使用 render_to_string() 渲染的。 但是,电子邮件内容直接
我是我的 django 应用程序,我想将静态 html 页面加载到主模板中。这个静态 html 页面是一个电子邮件模板。我的目标是在我的主模板中编辑这封电子邮件。电子邮件 html 页面在 url.p
我需要在我的 ProjectBill 模型上实现一个 _to_pdf 方法,它会在这个地方生成我的账单的 PDF: /public/xls/bills/project_#{project.number
我是一名优秀的程序员,十分优秀!