Django choices下拉列表绑定实例
作者:老姥 时间:2023-05-06 14:08:52
Models内容
from django.db import models
from django import forms
# Create your models here.
class SysConfigForm(forms.Form):
DatabaseType = forms.ChoiceField(choices=[('sqlserver', 'SQLServer'), ('oracle', 'Oracle')])
class UserInfo(forms.Form):
vip_type = ((0, u'普通用户'),(1, u'高级用户'),)
vip = forms.CharField(widget=forms.widgets.Select(choices=vip_type,attrs={'class':'form-control','with':'25px'}), )
class Months(forms.Form):
list = ((1,u'一月'),(2,u'二月'),(3,u'三月'),(4,u'四月'),(5,u'五月'),(6,u'六月'),
(7, u'七月'),(8,u'八月'),(9,u'九月'),(10,u'十月'),(11,u'十一月'),(12,u'十二月'),)
obj_month = forms.CharField(widget=forms.widgets.Select(choices=list, attrs={'class': 'form-control'}), )
class UserUsesSourceForm(forms.Form):
# some fields here
SOURCES_CHOICES = (
('A', 'A'),
('E', 'E'),
)
username = forms.CharField(label=("Username"), max_length=30, help_text = ("Required"))
provider = forms.ChoiceField(widget=forms.Select(), choices=SOURCES_CHOICES, initial=SOURCES_CHOICES[1])
Views内容
from django.shortcuts import render,HttpResponse
from polls import models
from django.template.loader import get_template
# Create your views here.
def index(request):
obj = models.UserInfo()
if request.method == 'POST':
user_obj = models.UserInfo(request.POST)
if user_obj.is_valid():
print(user_obj.clean())
else:
user_error = user_obj.errors
print (user_error)
return render(request,'index.html',{'obj':obj,'user_error':user_error})
months = models.Months()
return render(request,'index.html',{'obj':obj,'months':months})
#获取下拉列表选中记录
def Test01(request):
template = get_template('test01.html')
form = models.UserUsesSourceForm(initial={"username": request.user.username, 'provider': models.UserUsesSourceForm.SOURCES_CHOICES[1]})
#return render_to_response('update_datasource.html', context_instance=RequestContext(request, params))
html = template.render(locals())
return HttpResponse(html)
Test页面内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
{% if form.non_field_errors %}
<p>
{% for error in form.non_field_errors %}
<div class="text-error">{{ error|escape }}</div>
{% endfor %}
</p>
{% endif %}
<div class="control-group">
<label class="control-label" for="id_provider">Data source</label>
<div class="controls">
{{form.provider}}
</div>
</div>
</form>
</body>
</html>
显示结果为
补充知识:django前端页面下拉选择框默认值设置
1,前端样式
2,前端html代码
<select name="row.status">
<option value="ON" {% if row.status == 'ON' %} selected="selected" {% endif %}>ON</option>
<option value="OFF" {% if row.status == 'OFF' %} selected="selected" {% endif %}>OFF</option>
</select>
来源:https://blog.csdn.net/i168wintop/article/details/80044301
标签:Django,choices,下拉列表,绑定
0
投稿
猜你喜欢
CentOS7.4 源码安装MySQL8.0的教程详解
2024-01-20 01:44:47
解决SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法
2024-01-22 08:24:14
在任意字符集下正常显示网页的方法二(续)
2023-11-22 17:36:14
详解Hadoop2.7.2 编译64位源码
2023-08-29 13:41:59
pytorch实现textCNN的具体操作
2022-08-28 17:40:00
在主机商的共享服务器上部署Django站点的方法
2021-03-20 22:41:34
在Python中测试访问同一数据的竞争条件的方法
2023-03-29 15:04:06
Python numpy.transpose使用详解
2021-10-17 16:51:10
人工智能学习PyTorch教程之层和块
2021-12-05 09:36:15
JS实现移动端判断上拉和下滑功能
2023-07-13 22:05:21
vue项目运行或打包时,频繁内存溢出情况问题
2024-05-02 16:53:45
python实现把二维列表变为一维列表的方法分析
2021-11-30 07:13:44
解决Python selenium get页面很慢时的问题
2022-08-29 02:08:22
Python类如何定义私有变量
2023-04-18 11:23:23
python基于SMTP发送QQ邮件
2023-04-07 21:11:51
python 图像处理画一个正弦函数代码实例
2021-08-10 22:17:51
Django 创建/删除用户的示例代码
2022-09-30 08:59:29
swoole和websocket简单聊天室开发
2023-10-23 07:20:24
sqlserver给表添加新字段、给表和字段添加备注、更新备注及查询备注(sql语句)
2024-01-24 10:54:57
js表单检测数字
2007-10-12 13:52:00