一、HTML中 | safe 可在后台用以下方式处理
1 text = "<input type'text' />"
2 from django.utils.safestring import mark_safe
3 text = mark_safe(text)
二、views.py操作
1 from django.shortcuts import render
2
3 from django import forms
4 from django.forms import fields,widgets
5 from app01 import models
6 class LoveForm(forms.Form):
7 price = fields.IntegerField()
8 user_id = fields.IntegerField(
9 # widget=widgets.Select(choices=[(1,'alex'),(2,'xialv'),(3,'sb'),])
10 # 数据库取值
11 widget=widgets.Select()
12 )
13 # 设置动态绑定,随时更新数据
14 def __init__(self,*args,**kwargs):
15 # super必须在前,拷贝所有的静态字段,复制给self.fields
16 super(LoveForm,self).__init__(*args,**kwargs)
17 self.fields['user_id'].widget.choices = models.UserInfo.objects.values_list('id','username')
18
19 def love(request):
20 obj = LoveForm()
21 return render(request,'love.html',locals())