python django:使用内联formset的错误

时间:2016-08-15 20:15:32

标签: python django django-forms

我有以下型号:

class Equipment(models.Model):
    asset_number = models.CharField(max_length = 200)
    serial_number = models.CharField(max_length = 200)

class Calibration(models.Model):
    cal_asset = models.ForeignKey(Equipment, on_delete = models.CASCADE)
    cal_by = models.CharField(max_length = 200)
    cal_date = models.DateField()
    notes = models.TextField(max_length = 200)

并查看:

def default_detail (request, equipment_id):
    equipment = Equipment.objects.get(id = equipment_id)
    if request.method == "POST":
        if 'calibration' in request.POST:
            EquipmentInlineFormSet = inlineformset_factory(Equipment, Calibration, fields = ('cal_by', 'cal_dates', 'notes')
            formset = EquipmentInlineFormSet(request.POST, request.FILES, instance=equipment)
            if formset.is_valid():
                formset.save()
            return HttpResponseRedirect(reverse('calbase:default_detail', args=(post.id)))
    else:
        formset = EquipmentInlineFormSet(instance=equipment)
    return render(request, 'calbase/default_detail.html', {'formset' : formset})

此视图的模板:

<h1>{{ equipment.serial_number }}</h1>

{{equipment.serial_number}} -- {{equipment.asset_number}} <br>


calibration history:
{% for calibrations in equipment.calibration_set.all %}
<ul>
     <li>
         {{calibrations.cal_by}} -- {{calibrations.cal_date}} -- {{calibrations.notes}}
     </li>
</ul>
{% endfor %}

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form method="POST" action = "{% url 'calbase:default_detail' equipment.id %}">{% csrf_token %}
    {{ formset }}
    <button type="submit" class="save btn btn-default" name = "calibration">Save</button>
</form>

<a href="{% url 'calbase:default' %}">Back?</a>

此视图只是设备的详细视图,显示了一些信息(此设备的资产和序列号)。我正在尝试添加一个formset,让用户在此视图中为此设备添加校准记录并显示它们(如果有)。我了解到使用内联formset可能是要走的路。但是,在逐步执行documentation之后,我有一个

  

equipmentInlineFormSet = EquipmentInlineFormSet(request.POST,request.FILES,instance = equipment)

     

SyntaxError:语法无效

错误,即使我检查确保没有拼写错误。我只想弄清楚我在这里做错了什么。

0 个答案:

没有答案