这个问题是关于在选择器的选择中正确呈现html。这是我的代码:
models.py
from django.db import models
class Test(models.Model):
area = models.FloatField(choices=((0, '0 mm²'), (0.5, '0.5 mm²')), default=0)
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.forms import ModelForm
from models import Test
class TestForm(ModelForm):
class Meta:
model = Test
def test(request):
form = TestForm()
return render_to_response('test.html', RequestContext(request, {'form': form}))
的test.html
{{ form.area }}
{{ form.area|safe }}
{% autoescape off %}{{ form.area }}{% endautoescape %}
以上三个选项均未成功将选择器选项正确显示为0mm²而非0 mm²
。怎么解决这个问题?
答案 0 :(得分:1)
使用mark_safe
函数使Django不会在模板中转义字符串
from django.utils.safestring import mark_safe
from django.db import models
class Test(models.Model):
area = models.FloatField(choices=((0, mark_safe('0 mm²')), (0.5, mark_safe('0.5 mm²'))), default=0)