Home > Mobile >  Why individually rendered form fields not posting in Django?
Why individually rendered form fields not posting in Django?

Time:01-21

I have a class based UpdateView that works fine if I render the form as {{ form.as_p }}. It updating the values, but looks bad.

If I try to render the fields individually it does not updating the values.

I can't figure out how to fix it, I'm not so familiar with class based views. I don't think there is a validation error (i don't know how to check it in class based views) because {{ form.as_p }} works well. Thank you in advance if someone can help me out!

models.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)

views.py

class UpdateLeaderFeedbackView(SuccessMessageMixin, UpdateView):
    model = LeaderFeedback
    form_class = PeriodicLeaderFeedbackForm
    template_name = 'performance/periodic/leader_feedback_edit.html

forms.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'}),

urls.py

app_name = 'performance'
urlpatterns = [

    ...
    path('periodic/leader_feedback_edit/<pk>', login_required(UpdateLeaderFeedbackView.as_view()), name='update_leaderfeedback'),
    ...

leader_feedback_edit.html

<form method="POST">
{% csrf_token %}

<div >
    <div >
        <div >
            <h6 >Cél 1:</h6>
                {{ object.goals01 }}
        </div>
        <div >
            <h6 >Kitűzött határidő:</h6>
                {{ object.goals01_deadline }}
        </div>
        <div >
            <h6 >Értékelés:</h6>
                 {{ form.goals01_value }}
         </div>
         {{ form.leader_comment }}
     </div>
     <input type="submit" value="submit" ></input>
 </div>
</form>

CodePudding user response:

First, you need to include in your HTML form tags. Also, you need to take care of csrf_token.

<form method="post">
{% csrf_token %}
<div >
<div >
    <div >
        <h6 >Cél 1:</h6>
            {{ object.goals01 }}
    </div>
    <div >
        <h6 >Kitűzött határidő:</h6>
            {{ object.goals01_deadline }}
    </div>
    <div >
        <h6 >Értékelés:</h6>
             {{ form.goals01_value }}
     </div>
     {{ form.leader_comment }}
 </div>
<button type="submit" >Submit</button>
</div>
</form>
  • Related