Django Model Form响应200,但失败

时间:2020-07-27 20:31:18

标签: python django django-forms

我有一个Django ModelForm,如下所示:

class MeasurementForm(ModelForm):
    class Meta:
        model = Measurement
        fields = ['weight', 'height', 'back_length', 'side_length', 'girth', 'food_consumption', 'measurement_type']

我在create_measurement视图中使用该表单,如下所示:

def create_measurement(request, pup_id):
    pup = Pup.objects.get(id=pup_id)

    if request.method == 'POST':
        form = MeasurementForm(request.POST)

        if form.is_valid():
            measurement = form.save(commit=False)
            measurement.measured_pup = pup

            measurement.save()
            return redirect('/pup/{{ pup.id }}')
    else:
        form = MeasurementForm()
    
    return render(request, 'create_measurement.html', {'form': form, 'pup_id': pup.id})

与html一起使用

<form action="/{{ pup_id }}/create-measurement/" method="POST">
                    {% csrf_token %}
                    {% load widget_tweaks %}

                    <small class="text-danger">{{ form.non_field_errors }}</small>

                    <div class="row">
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.weight.errors }}</small>
                            <label for="{{ form.weight.id_for_label }}">Weight:</label>
                            {{ form.weight|add_class:"form-control" }}
                        </div>
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.height.errors }}</small>
                            <label for="{{ form.height.id_for_label }}">Height:</label>
                            {{ form.height|add_class:"form-control" }}
                        </div>
                    </div>

                    <div class="row">
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.back_length.errors }}</small>
                            <label for="{{ form.back_length.id_for_label }}">Back Length:</label>
                            {{ form.back_length|add_class:"form-control" }}
                        </div>
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.side_length.errors }}</small>
                            <label for="{{ form.side_length.id_for_label }}">Side Length:</label>
                            {{ form.side_length|add_class:"form-control" }}
                        </div>
                    </div>

                    <div class="row">
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.girth.errors }}</small>
                            <label for="{{ form.girth.id_for_label }}">Girth:</label>
                            {{ form.girth|add_class:"form-control" }}
                        </div>
                        <div class="form-group col-md-6">
                            <small class="text-danger">{{ form.measurement_type.errors }}</small>
                            <label for="{{ form.measurement_type.id_for_label }}">Measurement Type:</label>
                            {{ form.measurement_type|add_class:"form-control" }}
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">Add Measurement</button>
                </form>

提交表单时,我会从控制台收到状态200(确定),但是该表单不会保存,并且我也不会像应有的那样重定向到小狗详细信息页面。我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

{{ pup.id }}是Django模板语法,应该与模板引擎一起使用。如果您需要简单的变量插值,则可以改为使用 f-string

def create_measurement(request, pup_id):
    pup = Pup.objects.get(id=pup_id)
    if request.method == 'POST':
        form = MeasurementForm(request.POST)
        if form.is_valid():
            ...
            return redirect(f'/pup/{pup.id}')