欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django choices下拉列表綁定實(shí)例

 更新時(shí)間:2020年03月13日 10:25:57   作者:老姥  
這篇文章主要介紹了Django choices下拉列表綁定實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

Models內(nèi)容

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'高級(jí)用戶'),)
  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內(nèi)容

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頁(yè)面內(nèi)容

<!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>

顯示結(jié)果為

補(bǔ)充知識(shí):django前端頁(yè)面下拉選擇框默認(rèn)值設(shè)置

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下拉列表綁定實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中的xlrd模塊使用整理

    Python中的xlrd模塊使用整理

    今天給大家?guī)?lái)的文章是關(guān)于Python的相關(guān)知識(shí),文章圍繞著xlrd模塊的使用展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 使用FastCGI部署Python的Django應(yīng)用的教程

    使用FastCGI部署Python的Django應(yīng)用的教程

    這篇文章主要介紹了使用FastCGI部署Python的Django應(yīng)用的教程,FastCGI也是被最廣泛的應(yīng)用于Python框架和服務(wù)器連接的模塊,需要的朋友可以參考下
    2015-07-07
  • Python實(shí)現(xiàn)Dijkstra算法

    Python實(shí)現(xiàn)Dijkstra算法

    今天小編就為大家分享一篇關(guān)于Python實(shí)現(xiàn)Dijkstra算法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Python多進(jìn)程與多線程的使用場(chǎng)景詳解

    Python多進(jìn)程與多線程的使用場(chǎng)景詳解

    這篇文章主要給大家介紹了關(guān)于Python多進(jìn)程與多線程使用場(chǎng)景的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python通過(guò)post提交數(shù)據(jù)的方法

    python通過(guò)post提交數(shù)據(jù)的方法

    這篇文章主要介紹了python通過(guò)post提交數(shù)據(jù)的方法,涉及Python使用post方式傳遞數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • python添加模塊搜索路徑和包的導(dǎo)入方法

    python添加模塊搜索路徑和包的導(dǎo)入方法

    今天小編就為大家分享一篇python添加模塊搜索路徑和包的導(dǎo)入方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python實(shí)現(xiàn)字符串的逆序 C++字符串逆序算法

    Python實(shí)現(xiàn)字符串的逆序 C++字符串逆序算法

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)字符串的逆序,C++將字符串逆序輸出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法詳解

    python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法詳解

    這篇文章主要介紹了python內(nèi)存監(jiān)控工具memory_profiler和guppy的用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python識(shí)別文字(基于tesseract)代碼實(shí)例

    python識(shí)別文字(基于tesseract)代碼實(shí)例

    這篇文章主要介紹了python識(shí)別文字(基于tesseract)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python輸入若干整數(shù)求和方式

    Python輸入若干整數(shù)求和方式

    這篇文章主要介紹了Python輸入若干整數(shù)求和方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評(píng)論