django动态表单设置max_length属性

发布于 2025-01-11 02:34:13 字数 4352 浏览 4 评论 0原文

我正在为我的应用程序创建一个“表单管理”系统。

我正在使用自定义表单“工厂”方法动态创建表单。

表单数据位于 json 文件中。

我可以创建一个 forms.CharField 并设置 label、required、initial 和 help_text 属性。

当我尝试设置 max_length 属性时,我没有收到任何错误消息,但生成的 HTML 不包含 max_length 属性。

在静态(类)形式中定义为

class SearchAccountForm(forms.Form):
    provider_code = forms.CharField(
        label='Provider:', 
        max_length=100, 
        required=True, 
        widget=forms.TextInput(attrs={'class': 'form-control'}))

结果 HTML 包含 max_length 属性。

<label for="id_provider_code">Provider:</label>
</th><td><input type="text" name="provider_code" class="form-control" maxlength="100" required id="id_provider_code">

那么 max_length 是怎么回事?

Json 文件

{
    "form1": [
        {
            "fld_name": "customer_name",
            "fld_type": "CharField",
            "fld_label": "Cust Name",
            "fld_required": "False",
            "fld_maxLength": 5,
            "initial": "Dr John"
        },
        {
            "fld_name": "customer_number",
            "fld_type": "CharField",
            "fld_label": "Cust #",
            "fld_required": "True",
            "fld_maxLength": 15,
            "help_text": "Enter account number"
        },
        {
            "fld_name": "customer_type",
            "fld_type": "CharField",
            "fld_label": "Customer Type",
            "fld_required": "False"
        }
    ]
}

和 forms.py 工厂方法

from django import forms
import json

def dynfrm():
    f = open('blog/frmJson/frm1.json')

    data = json.load(f)
    fields = {}
    for i in data['form1']:     ## form1 = form name in json file
        print(i)
        ## add to fields list
        if i['fld_type'] == 'CharField':
            fields[i["fld_name"]] = forms.CharField()
            if 'fld_label' in i:
                fields[i["fld_name"]].label = i["fld_label"]
            if 'fld_required' in i:
                if i["fld_required"] == 'False':
                    fields[i["fld_name"]].required = False
                else:
                    fields[i["fld_name"]].required = True
            if 'initial' in i: fields[i["fld_name"]].initial = i["initial"]
            if 'help_text' in i: fields[i["fld_name"]].help_text = i["help_text"]
## next line not working
            if 'fld_maxLength' in i: fields[i["fld_name"]].max_length = i["fld_maxLength"]

            fields[i["fld_name"]].widget = forms.TextInput()

    return type('DynForm',  # form name is irrelevant
                (forms.BaseForm,),
                {'base_fields': fields})    

我的视图

def vdynfrm(request):
    if request.method == 'POST':
        form = dynfrm(request.POST)
        if form.is_valid():
            pass
            ## all good
    else:
        form = dynfrm()

    ##return render(request, "blog/dfrm.html",{'form': form})
    return render(request, "blog/searchAccount.html",{'form': form})

和生成的 HTML

<div class="form-group">
   <form action="/searchAccount/" method="post">
      <table>
         <tr>
            <th><label for="id_customer_name">Cust Name:</label></th>
            <td><input type="text" name="customer_name" value="Dr John" id="id_customer_name">/td> 
         </tr>
         <tr>
            <th><label for="id_customer_number">Cust #:</label></th>
            <td><input type="text" name="customer_number" required id="id_customer_number"><br> 
                <span class="helptext">Enter account number</span></td>
            </tr>
            <tr>
               <th><label for="id_customer_type">Customer Type:</label></th>
               <td><input type="text" name="customer_type" id="id_customer_type"></td>
            </tr>    
      </table>
         
      <input type="submit" value="Submit">
   </form>
</div>

I am creating a 'Forms Management' system for my application.

I am creating a forms dynamically using a custom form 'factory' method.

Form data is in a json file.

I can create a forms.CharField and set the label, required, initial and help_text properties.

When I try to set the max_length property I do not get any error message, but the resulting HTML does not contain the max_length attribute.

In static (class) forms defined as

class SearchAccountForm(forms.Form):
    provider_code = forms.CharField(
        label='Provider:', 
        max_length=100, 
        required=True, 
        widget=forms.TextInput(attrs={'class': 'form-control'}))

The resulting HTML contains the max_length attribute.

<label for="id_provider_code">Provider:</label>
</th><td><input type="text" name="provider_code" class="form-control" maxlength="100" required id="id_provider_code">

So what's up with max_length??

Json file

{
    "form1": [
        {
            "fld_name": "customer_name",
            "fld_type": "CharField",
            "fld_label": "Cust Name",
            "fld_required": "False",
            "fld_maxLength": 5,
            "initial": "Dr John"
        },
        {
            "fld_name": "customer_number",
            "fld_type": "CharField",
            "fld_label": "Cust #",
            "fld_required": "True",
            "fld_maxLength": 15,
            "help_text": "Enter account number"
        },
        {
            "fld_name": "customer_type",
            "fld_type": "CharField",
            "fld_label": "Customer Type",
            "fld_required": "False"
        }
    ]
}

and the forms.py factory method

from django import forms
import json

def dynfrm():
    f = open('blog/frmJson/frm1.json')

    data = json.load(f)
    fields = {}
    for i in data['form1']:     ## form1 = form name in json file
        print(i)
        ## add to fields list
        if i['fld_type'] == 'CharField':
            fields[i["fld_name"]] = forms.CharField()
            if 'fld_label' in i:
                fields[i["fld_name"]].label = i["fld_label"]
            if 'fld_required' in i:
                if i["fld_required"] == 'False':
                    fields[i["fld_name"]].required = False
                else:
                    fields[i["fld_name"]].required = True
            if 'initial' in i: fields[i["fld_name"]].initial = i["initial"]
            if 'help_text' in i: fields[i["fld_name"]].help_text = i["help_text"]
## next line not working
            if 'fld_maxLength' in i: fields[i["fld_name"]].max_length = i["fld_maxLength"]

            fields[i["fld_name"]].widget = forms.TextInput()

    return type('DynForm',  # form name is irrelevant
                (forms.BaseForm,),
                {'base_fields': fields})    

my view

def vdynfrm(request):
    if request.method == 'POST':
        form = dynfrm(request.POST)
        if form.is_valid():
            pass
            ## all good
    else:
        form = dynfrm()

    ##return render(request, "blog/dfrm.html",{'form': form})
    return render(request, "blog/searchAccount.html",{'form': form})

and the resulting HTML

<div class="form-group">
   <form action="/searchAccount/" method="post">
      <table>
         <tr>
            <th><label for="id_customer_name">Cust Name:</label></th>
            <td><input type="text" name="customer_name" value="Dr John" id="id_customer_name">/td> 
         </tr>
         <tr>
            <th><label for="id_customer_number">Cust #:</label></th>
            <td><input type="text" name="customer_number" required id="id_customer_number"><br> 
                <span class="helptext">Enter account number</span></td>
            </tr>
            <tr>
               <th><label for="id_customer_type">Customer Type:</label></th>
               <td><input type="text" name="customer_type" id="id_customer_type"></td>
            </tr>    
      </table>
         
      <input type="submit" value="Submit">
   </form>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2025-01-18 02:34:13

仅当您将上下文正确发送到模板文件时,max_length 属性才起作用。

您的 forms.py

class SearchAccountForm(forms.Form):
    provider_code = forms.CharField(
        label='Provider:',
        max_length=100,
        required=True,
        widget=forms.TextInput(attrs={'class': 'form-control'}))

使用基于 function 的视图:


def home(request):
    if request.method == 'POST':
        form = SearchAccountForm(request.POST)
        if form.is_valid():
            provider_c= form.cleaned_data['provider_code']
            print('Provider Code :',provider_c)
            return HttpResponseRedirect('/thanks/')
    else:
        form = SearchAccountForm()

    return render(request, 'home/index.html', {'form': form})

def thanks(req):
    return render(req, 'home/thanks.html')

如果您忘记为 get 请求方法提供 else 条件,那么您将不会收到 django 的 内置错误消息以及 max_length 等。

使用基于类的视图,可以轻松处理它:

from django.views.generic.edit import FormView

class Home(FormView):
    template_name = 'home/index.html'
    form_class = SearchAccountForm
    success_url = '/thanks/'

    def form_valid(self, form):
        print(form)
        print('Provider Code : ', form.cleaned_data['provider_code'])
        return super().form_valid(form)

def thanks(req):
    return render(req, 'home/thanks.html')

从上面的两个示例来看,max_length 属性工作正常,因为它的get 请求已得到处理。

检查你的views.py,它可能会有所帮助。

The max_length property only works when you send the context correctly to your template file.

your forms.py

class SearchAccountForm(forms.Form):
    provider_code = forms.CharField(
        label='Provider:',
        max_length=100,
        required=True,
        widget=forms.TextInput(attrs={'class': 'form-control'}))

With function based view:


def home(request):
    if request.method == 'POST':
        form = SearchAccountForm(request.POST)
        if form.is_valid():
            provider_c= form.cleaned_data['provider_code']
            print('Provider Code :',provider_c)
            return HttpResponseRedirect('/thanks/')
    else:
        form = SearchAccountForm()

    return render(request, 'home/index.html', {'form': form})

def thanks(req):
    return render(req, 'home/thanks.html')

If you forget to give else condition for get request method, so you will not receive django's inbuild error messages as well as max_length etc.

With Class based view it can be handled easily:

from django.views.generic.edit import FormView

class Home(FormView):
    template_name = 'home/index.html'
    form_class = SearchAccountForm
    success_url = '/thanks/'

    def form_valid(self, form):
        print(form)
        print('Provider Code : ', form.cleaned_data['provider_code'])
        return super().form_valid(form)

def thanks(req):
    return render(req, 'home/thanks.html')

From both the examples above max_length property is working properly because its get request got handled.

Check your views.py, it may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文