温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Django choices下拉列表绑定实例

发布时间:2020-10-22 23:26:00 来源:脚本之家 阅读:169 作者:老姥 栏目:开发技术

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 choices下拉列表绑定实例

补充知识:django前端页面下拉选择框默认值设置

1,前端样式

Django choices下拉列表绑定实例

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下拉列表绑定实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI