English 中文(简体)
为什么Django的只读形式田地是一个坏的想法?
原标题:Why are read-only form fields in Django a bad idea?

我一直在寻找一种办法,以建立一个只读形式的领域,我在此专题上发现的每一条都有一个声明,即“这是坏的想法”。 现在,就个人形式而言,我可以理解,有其他办法解决这一问题,但是,在模式中只使用一种读写法,似乎是一种完全自然的想法。

考虑采用一门教师级书,使教师能够进入所有学生(指复数学生)的年级,并有一个单一的SUBMIT。 模式可以使所有学生的年级都更加融通,使学生的名字只读,而年级是不容置疑的领域。 象检查错误和错误报告的错误的威力和方便性一样,你掌握了一种模式,但将学生的名字留在这种表格中是虚幻的。

由于德詹戈专家的共识是,仅读形式领域是一个坏的观念,我想知道,标准 d延最佳做法是上文学生级的例子?

最佳回答

你不希望这样做的原因是,有人可以改变残疾领域,使残疾状况得以形成,然后提交。 你们必须改变储蓄功能,不要添加“可处置”的数据。

这样做的标准方法是不把名字放在投入中,而是把它作为案文显示。

<form>
    <div>
        <label>Name</label>
        <p>Johnny Five</p>
    </div>
    <div>
        ....

这一点在django是不可能的。

我说,如果你真心相信你的用户基数不会与事情一起“主题”,那么,如果一个有可能敏感数据的公众在网站面前,就会消失。

问题回答

在使用残疾领域时,如果形式不能验证,你还需要确保其人口保持正确。 这里,我的方法也考虑到恶意企图改变所提交的数据:

class MyForm(forms.Form):

    MY_VALUE =  SOMETHING 
    myfield = forms.CharField(
        initial=MY_VALUE,
        widget=forms.TextInput(attrs={ disabled :  disabled })

    def __init__(self, *args, **kwargs):

        # If the form has been submitted, populate the disabled field
        if  data  in kwargs:
            data = kwargs[ data ].copy()
            self.prefix = kwargs.get( prefix )
            data[self.add_prefix( myfield )] = MY_VALUE
            kwargs[ data ] = data

        super(MyForm, self).__init__(*args, **kwargs) 

就学生/年级而言,我提出了解决办法,在这种解决办法中,学生是不可调试的田地,能够按要求加以编辑和更新。 部分内容与本

我将学生的科目和年级的表格结合起来。 py using zip function.

def grade_edit(request, id):
    student = student.objects.get(id=id)
    grades = grades.objects.filter(studentId=id)
    gradeformset = GradeFormSet(request.POST or None)
    if request.POST:
        gradeformset = GradeFormSet(request.POST, request.FILES, instance=student)
        if gradeformset.is_valid():
            gradeformset.save()
            grades = grades.objects.filter(studentId=id)
            return render(request,  grade_details.html , { student : student,  grades : grades})
    else:
        gradeformset = GradeFormSet(instance=student)
        grades = grades.objects.filter(studentId=id)
        zips = zip(grades, gradeformset)
    return render(request,  grade_edit.html , { zips : zips,  student : student,  gradeformset : gradeformset })

我的模板看着这样的情况。

<table>
         <tr>
     {% for field in gradeformset.forms.0 %}
          {% if not field.is_hidden %}
               <th>{{ field.label }}</th>
          {% endif %}
     {% endfor %}
     </tr>
     {% for f in gradeformset.management_form %}
          {{ f }}
     {% endfor %}
     {% for student, gradeform in zips %}
          <tr>
             {% for hidden in form.hidden_fields %}
                 {{ hidden }}
             {% endfor %}
             <td> {{ student.name }} </td>
             <td> {{ gradeform.gradeA }} </td>
             <td> {{ gradeform.gradeB }} </td>
          </tr>
     {% endfor %}
</table>

You can read more about Django formset here http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签