【问题标题】:Unicode and the Django adminUnicode 和 Django 管理员
【发布时间】:2016-07-18 13:20:04
【问题描述】:

如果我想通过管理员将 unicode 输入到我的数据库中,我该怎么做?如果我想在数据库中输入 2^5 以将其作为上标输出到模板上,我可以通过管理员执行此操作吗?我试过只输入 \u0034\u2075 但它不起作用。任何帮助将不胜感激。

观看次数

def recipes(request):
    recipe_list = Recipe.objects.all()
    ingredient_list = Ingredient.objects.all()
    context = {
        'recipe_list': recipe_list,
        'ingredient_list': ingredient_list,
    }
return render(request, "recipes/test.html", context)

模板

<div class="container">
    <h1>{{ var }}</h1>
    {% for q in recipe_list %}
        <h1>{{ q }}</h1><br />
        {% for i in ingredient_list %}

            {{ i }}<br />

        {% endfor %}
    {% endfor %}
 </div>

我尝试将一些 unicode 直接添加到视图中,效果很好。我在视图函数中添加了 var = u'\u0034\u2075' 并将其传递到上下文字典中,并在模板中简单地添加了一个 {{ var }} 并且效果很好,但我似乎无法让它工作拉它通过管理员从数据库中添加它。

【问题讨论】:

  • 您是否尝试过像 (x³) 这样直接粘贴 Unicode 值?
  • 我不确定你的意思。
  • 你期望渲染的模板显示什么,你实际得到了什么? Recipe 与 Ingredient 有什么关系?循环遍历配方循环中的成分似乎很奇怪。这意味着您将显示每个食谱的每种成分。

标签: django django-admin python-unicode


【解决方案1】:

在管理员中输入实际的 unicode 字符(例如4⁵)应该可以工作。 Django 在任何地方都支持 unicode,as long as your database supports it。

【讨论】:

  • 是的,我使用的是 postgresql,并且编码设置为 utf8,所以它应该支持它,但是当我尝试使用数据库输出到模板时,它只显示为 \u0034\u2075。
猜你喜欢
  • 2014-02-21
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 2020-05-09
  • 1970-01-01
  • 2010-10-12
相关资源
最近更新 更多