如何以Django形式显示'auto_now_add'字段?

时间:2018-06-27 14:05:40

标签: django python-3.x django-forms

我想知道如何在Django前端表单中显示“ auto_now_add”字段(以及作为禁用字段)。 我附上一些相关代码:

# models.py
class ModelA(models.Model):
    name = models.CharField('Name', max_length=100)
    inserted = models.DateTimeField('Inserted ', auto_now_add=True)

# forms.py
class ProcessModelAForm(forms.ModelForm):
    class Meta:
        model = ModelA
        fields = '__all__'
        widgets = {
            "name": forms.TextInput({
                "class": "form-control",
                "disabled": True,
            }),
            "inserted": forms.DateTimeInput({
                "class": "form-control",
                "disabled": True,
            }),
        }

# views.py
def process_model_a(request, pk):
    instance_model_a = get_object_or_404(ModelA, id=pk)
    process_form = ProcessModelAForm(instance=instance_model_a)
    if request.method == 'POST':
        # ...
    return render(request, 'myapp/process_model_a.html', {'process_form ': process_form , 'instance':instance_model_a , } )

# process_model_a.html
  <form method="post" class="form-horizontal">
    {% csrf_token %}
    {% for field in process_form %}
    <div class="form-group form-group-lg">
        <label for="{{ field.id_for_label }}" class="col-sm-2 control-label">{{field.label}}</label>
        <div class="col-sm-6">
            {{ field }}
        </div>
        <div class="col-sm-4">
        {{ field.errors }}
        </div>
    </div>
    {% endfor %}
    <div class="form-group">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary btn-lg center-block">Process</button>
        </div>
    </div>
  </form>

页面“ process_model_a.html”仅显示“名称”字段。 如何显示“插入”字段? 谢谢。

0 个答案:

没有答案