0

这个问题是关于在选择器的选择中正确呈现 html。这是我的代码:

模型.py

from django.db import models
class Test(models.Model):
    area = models.FloatField(choices=((0, '0 mm²'), (0.5, '0.5 mm²')), default=0)

视图.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}))

测试.html

{{ form.area }}
{{ form.area|safe }}
{% autoescape off %}{{ form.area }}{% endautoescape %}

以上三个选项都不能成功地将选择器选项正确显示为0 mm²而不是0 mm². 如何解决?

4

1 回答 1

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)
于 2013-11-07T09:34:28.797 回答