【发布时间】:2020-08-11 03:22:46
【问题描述】:
背景
我在表单中使用了 'mark_safe()' 来传递 HTML 指令并在我的表单标签中显示要点:
class FormFood(forms.Form):
CHOICES = [ (1,'Yes'), (2, 'No')]
response = forms.ChoiceField(widget=forms.RadioSelect,
label=mark_safe("Do you like any of these foods? <ul><li>Pasta</li><li>Rice</li><li>Cheese</li></ul>"), choices=CHOICES)
这输出为:
问题
正如您在上面看到的,尽管我不希望这样,但项目符号现在也出现在“是”和“否”旁边。检查元素显示这是因为它们也是在无序列表标签内构建的(尽管我的 ul 标签在上面的标签中以 'cheese' 结尾):
<form method="POST">
<label for="id_response_0">Do you like any of these foods?
<ul>
<li>Pasta</li>
<li>Rice</li>
<li>Cheese</li>
</ul>
</label>
<ul id="id_response">
<li>
<label for="id_response_0">
<input type="radio" name="response" value="1" required="" id="id_response_0"> Yes
</label>
</li>
<li>
<label for="id_response_1">
<input type="radio" name="response" value="2" required="" id="id_response_1"> No
</label>
</li>
</ul>
<input type="hidden" name="csrfmiddlewaretoken" value="FaOTY4WlVLFMl3gNtut8BJihJKub1Is0wRJRxxLck1e2eJocJVXRiGoOLDr9jdvx">
<input type="submit">
</form>
有没有办法可以阻止这种情况,只保留“意大利面”、“米饭”和“奶酪”上的要点,同时从“是”和“否”中删除要点/列表?
【问题讨论】:
-
This 可能会有所帮助。
-
我明白了,用 CSS 添加一个类和目标。那应该行得通。我想知道是否可以从后端做一些更优雅的事情,但前端修复也可以。谢谢
-
你的 ul 标签结尾不正确吗?
<</ul> -
嗨 MogitC,好发现!只是解决了这个问题。不幸的是,在这里而不是在我的代码中转移时是一个错字
标签: python html django python-3.x django-forms