Django choices下拉列表绑定实例

 更新时间:2020年03月13日 10:25:57   作者:老姥  
这篇文章主要介绍了Django choices下拉列表绑定实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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> 

以上这篇Django choices下拉列表绑定实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python实现AI自动玩俄罗斯方块游戏

    Python实现AI自动玩俄罗斯方块游戏

    提到《俄罗斯方块》,那真是几乎无人不知无人不晓。其历史之悠久,可玩性之持久,能手轻轻一挥,吊打一大波游戏。本文将利用Python实现俄罗斯方块进阶版—AI自动玩俄罗斯方块,感兴趣的可以学习一下
    2022-03-03
  • selenium+python自动化测试之多窗口切换

    selenium+python自动化测试之多窗口切换

    这篇文章主要介绍了selenium+python自动化测试之多窗口切换,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解

    python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解

    python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能。接下来通过本文给大家分享python中print()函数的“,”与java中System.out.print()函数中的“+”功能,需要的朋友参考下吧
    2017-11-11
  • python简单验证码识别的实现方法

    python简单验证码识别的实现方法

    这篇文章主要给大家介绍了关于python简单验证码识别的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • Python实现获取本地及远程图片大小的方法示例

    Python实现获取本地及远程图片大小的方法示例

    这篇文章主要介绍了Python实现获取本地及远程图片大小的方法,结合实例形式分析了Python使用PIL、urllib2及cStringIO模块获取本机或远程图片大小信息的相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • Pytorch:dtype不一致问题(expected dtype Double but got dtype Float)

    Pytorch:dtype不一致问题(expected dtype Double but&

    这篇文章主要介绍了Pytorch:dtype不一致问题(expected dtype Double but got dtype Float),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Python OpenCV实战之与机器学习的碰撞

    Python OpenCV实战之与机器学习的碰撞

    机器学习是人工智能的子集,为计算机以及其它具有计算能力的系统提供自动预测或决策的能力。本文主要介绍了OpenCV 提供的常见机器学习算法和技术,用于解决计算机视觉项目中的实际问题,需要的朋友可以参考一下
    2021-12-12
  • pytorch程序异常后删除占用的显存操作

    pytorch程序异常后删除占用的显存操作

    今天小编就为大家分享一篇pytorch程序异常后删除占用的显存操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python中字典创建、遍历、添加等实用操作技巧合集

    Python中字典创建、遍历、添加等实用操作技巧合集

    这篇文章主要介绍了Python中字典创建、遍历、添加等实用操作技巧合集,本文讲解了字典中常见方法列表、创建字典的五种方法、字典中键值遍历方法等内容,需要的朋友可以参考下
    2015-06-06
  • win7+Python3.5下scrapy的安装方法

    win7+Python3.5下scrapy的安装方法

    这篇文章主要介绍了win7+Python3.5下scrapy的安装方法
    2018-07-07

最新评论