为什么单独呈现的表单域在Django中不发布?

t9aqgxwy  于 2023-01-21  发布在  Go
关注(0)|答案(1)|浏览(130)

我有一个基于类的UpdateView,如果我把表单渲染成{{ form.as_p }},它会更新值,但是看起来很糟糕。
如果我尝试单独呈现字段,它不会更新值。
我不知道如何修复它,我不太熟悉基于类的视图。我不认为有一个验证错误(我不知道如何检查它在基于类的视图),因为{{ form.as_p }}工作得很好。谢谢你提前如果有人能帮助我!

    • 型号. py**
class LeaderFeedback(models.Model):

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

    employee = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="employed")
    leader = models.ForeignKey(User, on_delete=models.CASCADE, related_name="employer_leader")

    goals01 = models.CharField(max_length=100, null=True, blank=True)
    goals01_deadline = models.CharField(max_length=30, null=True, blank=True)
    goals01_value = models.IntegerField(null=True, blank=True)
    leader_comment = models.CharField(max_length=500, null=True, blank=True)
    • 查看次数. py**
class UpdateLeaderFeedbackView(SuccessMessageMixin, UpdateView):
    model = LeaderFeedback
    form_class = PeriodicLeaderFeedbackForm
    template_name = 'performance/periodic/leader_feedback_edit.html
    • 表单. py**
class Meta:
    model = LeaderFeedback
    fields = '__all__'
    
    goals_value =(
        (1, "Nem teljesült"),
        (2, "Részben teljesült"),
        (3, "Megfelelő"),
        (4, "Maximálisan teljesült"),
        )
            
    widgets = {            
        'goals01_value': forms.RadioSelect(choices=goals_value),
        'leader_comment': forms.Textarea(attrs={"rows":"3", 'class': 'form-control'}),
    • 网址. py**
app_name = 'performance'
urlpatterns = [

    ...
    path('periodic/leader_feedback_edit/<pk>', login_required(UpdateLeaderFeedbackView.as_view()), name='update_leaderfeedback'),
    ...
    • 负责人反馈编辑. html**
<form method="POST">
{% csrf_token %}

<div class="card shadow p-2 my-2">
    <div class="row">
        <div class="col-md-5">
            <h6 class="">Cél 1:</h6>
                {{ object.goals01 }}
        </div>
        <div class="col-md-3">
            <h6 class="">Kitűzött határidő:</h6>
                {{ object.goals01_deadline }}
        </div>
        <div class="col-md-4">
            <h6 class="">Értékelés:</h6>
                 {{ form.goals01_value }}
         </div>
         {{ form.leader_comment }}
     </div>
     <input type="submit" value="submit" class="btn btn-info my-3"></input>
 </div>
</form>
7eumitmz

7eumitmz1#

首先,你需要在你的HTML表单中包含标签,另外,你需要注意csrf_token。

<form method="post">
{% csrf_token %}
<div class="card shadow p-2 my-2">
<div class="row">
    <div class="col-md-5">
        <h6 class="">Cél 1:</h6>
            {{ object.goals01 }}
    </div>
    <div class="col-md-3">
        <h6 class="">Kitűzött határidő:</h6>
            {{ object.goals01_deadline }}
    </div>
    <div class="col-md-4">
        <h6 class="">Értékelés:</h6>
             {{ form.goals01_value }}
     </div>
     {{ form.leader_comment }}
 </div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

相关问题