Django- Form不保存给定的参数

时间:2017-05-22 06:04:11

标签: python django-forms

我不知道为什么但是我的views.py中的行recording.component=component没有保存作为参数给出的组件,我不明白原因。我唯一想到的是我使用的是smart_select。这就是我所拥有的:

我的views.py文件:

def create_recording(request, slug, inspection_id, component_id):
    inspection = get_object_or_404(Inspection, pk=inspection_id)
    plant = get_object_or_404(Plant, slug=slug)
    component=get_object_or_404(Component, pk=component_id)
    if request.method == "POST":
        form = RecordingForm(request.POST)
        if form.is_valid():
            recording = form.save(commit=False)
            recording.user = request.user
            recording.plant = plant
            recording.inspection=inspection
            recording.component=component
            recording.save()
            return redirect('data:inspection_detail', slug=plant.slug, inspection_id=inspection.id)
    else:
        form = RecordingForm()
    context = {'form': form, 'plant':plant,'inspection':inspection,}
    template = 'data/create_recording.html'
    return render(request, template, context)

my models.py:

class Recording(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
    inspection = models.ForeignKey(Inspection, on_delete=models.CASCADE)
    component = models.ForeignKey(Component)
    group = ChainedForeignKey(Group, chained_field="component", chained_model_field="component", show_all=False, auto_choose=True, sort=True)
    failure = ChainedForeignKey(Failure, chained_field="group", chained_model_field="group", show_all=False, auto_choose=True, sort=True)

    def __str__(self):
        return str(self.failure)

my forms.py:

class RecordingForm(forms.ModelForm):

    class Meta:
        model = Recording
        fields = ['group', 'failure',]

我的HTML:

<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=inspection.id component_id=1 %}">
     <button type="button" class="btn btn-success">
         <span class="glyphicon glyphicon-plus"></span> Chair
     </button>
</a>
<a href="{% url 'data:create_recording' slug=plant.slug inspection_id=inspection.id component_id=2 %}">
    <button type="button" class="btn btn-success">
        <span class="glyphicon glyphicon-plus"></span> Table
    </button>
</a>

和我的表单的html:

    <div class = "col-sm-7">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h4>Add new recording</h4>
            </div>
            <div class="panel-body">
                <form method='POST'>
                {% csrf_token %}
                {{ form|crispy }}
                <button type = 'submit' class="btn btn-success">Save</button>
                </form>
            </div>
        </div>
    </div>

和我的网址:

url(r'^plants/(?P<slug>[-\w]+)/inspection(?P<inspection_id>[0-9]+)/create_recording/(?P<component_id>[0-9]+)$', views.create_recording, name='create_recording'),

如果我需要添加更多信息,请告诉我。

0 个答案:

没有答案