form

Posted 人生苦短,我用python

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了form相关的知识,希望对你有一定的参考价值。

form操作动态select数据

forms.py

from django import forms
from django.forms import fields
from django.forms import widgets
from django.forms.models import ModelChoiceField
from app01 import models
class UserInfoForm(forms.Form):
    user = fields.CharField(
        required=True,
        widget=widgets.Textarea(attrs={class:c1})
    )
    pwd = fields.CharField(
        max_length=12,
        widget=widgets.PasswordInput(attrs={class:c1})
    )
    user_type = fields.ChoiceField(
        # choices=[(1,‘普通用户‘),(2,‘超级用户‘)],
        choices = [],
        # widget = widgets.Select
    )
    user_type2 = fields.CharField(widget=widgets.Select(choices=[]))
    #方式二,models.py中需要加上def __str__(self):return self.name
   user_type3
= ModelChoiceField( empty_label=请选择用户类型, to_field_name=name, queryset=models.UserType.objects.all() ) def __init__(self,*args,**kwargs): super(UserInfoForm, self).__init__(*args,**kwargs) self.fields[user_type].choices = models.UserType.objects.values_list(id,name) self.fields[user_type2].widget.choices = models.UserType.objects.values_list(id,name)

 

views.py

from django.shortcuts import render
from app01.forms import UserInfoForm
from app01 import models
# Create your views here.
def index(request):
    obj = UserInfoForm()
    # obj.fields[‘user_type‘].choices = models.UserType.objects.values_list(‘id‘,‘name‘)
    #类里面的静态字段,必须重新去数据库取值赋值,或者在forms.py中构造函数中取值
    return render(request,index.html,{obj:obj})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>
        {{ obj.user }}
    </p>
    <p>
        {{ obj.pwd }}
    </p>
    <p>
        {{ obj.user_type }}
        {{ obj.user_type2 }}
    </p>
</body>
</html>

 

以上是关于form的主要内容,如果未能解决你的问题,请参考以下文章

css 来自myStyles.css的[ArasLabs / custom-form-css]片段,显示应用于myIcon的样式

Xamarin.Forms XAML的辅助功能Code Snippet

如何将 react-hook-form 用于嵌套数组

Oracle Forms Builder:无法执行查询

什么是Taglib?

SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法