gpt4 book ai didi

python - Django 内联表单集不会这样做

转载 作者:行者123 更新时间:2023-12-04 14:59:37 26 4
gpt4 key购买 nike

我正在使用 Django 创建卡路里记录网站。一个人创建一个账户,可以开始记录他在特定日期消耗和燃烧的卡路里量,然后显示他在那一天消耗或燃烧的卡路里是否更多。
用户可以存储他经常吃的食物和他经常做的运动,这样当他添加数据时他就可以使用该项目
使用给定的 midels 用户可以添加食物和练习。
食品型号:

class Food(models.Model):
customer = models.ForeignKey(Customer, null=True, on_delete=models.CASCADE)
food_name = models.CharField(max_length=50, null=True, blank=True)
calories = models.IntegerField(null=True)
food_pic = models.ImageField(default="default_food.jpg", null=True, blank=True)

def __str__(self):
return f"{self.food_name}"

练习模型:

class Exercise(models.Model):
customer = models.ForeignKey(Customer, null=True, on_delete=models.CASCADE)
exercise_name = models.CharField(max_length=100, null=True, blank=True)
calories = models.IntegerField(null=True)
exercise_pic = models.ImageField(default="default_exercise.png", null=True, blank=True)

def __str__(self):
return f"{self.exercise_name}"

我使用以下两个模型来存储有关特定日期的信息:
DailyFood 模型:

class DailyFood(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
food_name = models.ForeignKey(Food, on_delete=models.CASCADE, null=True)
quantity = models.FloatField(null=True)
date = models.DateField(auto_now_add=True, null=True)

def __str__(self):
return f"{self.customer}, {self.date}"

日常锻炼模式:

class DailyExercise(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
exercise_name = models.ForeignKey(Exercise, on_delete=models.CASCADE, null=True)
quantity = models.FloatField(null=True) # number of hours done exercise
date = models.DateField(auto_now_add=True, null=True)

def __str__(self):
return f"{self.customer}, {self.date}"

在 views.py 文件中

def addData(request):
customer = request.user.customer
food_formset = inlineformset_factory(Customer, DailyFood, fields=('food_name', 'quantity', ))
exercise_formset = inlineformset_factory(Customer, DailyExercise, fields=('exercise_name', 'quantity', ))

f_formset = food_formset(instance=customer)
e_formset = exercise_formset(instance=customer)

if request.method == "POST":
f_formset = food_formset(request.POST, instance=customer)
e_formset = exercise_formset(request.POST, instance=customer)

if f_formset.is_valid():
mf_formset = f_formset.save(commit=False)
mf_formset.customer = customer
mf_formset.save()
else:
return HttpResponse("reached food_formset is not valid")

if e_formset.is_valid():
me_formset = e_formset.save(commit=False)
me_formset.customer = customer
me_formset.save()
return redirect('/')
else:
return HttpResponse("reached exercise_formset is not valid")
else:
foods = Food.objects.filter(customer=customer)
exercises = Exercise.objects.filter(customer=customer)


context = {'foods': foods, 'exercises': exercises, 'f_formset': f_formset, 'e_formset': e_formset}
return render(request, 'cc/add-data.html', context)

AddData html(表单部分):

<form action="" method="POST">
{% csrf_token %}
<div class="foods">
<p class="ad-accordion">Calories Consumed</p>
<div class="ad-accordion-content">
{% for form in f_formset %}
{{form}}
<br>
{% endfor %}
</div>
</div>
<div class="exercises">
<p class="ad-accordion">Calories Burnt</p>
<div class="ad-accordion-content">
{% for form in e_formset %}
{{form}}
<br>
{% endfor %}
</div>
</div>
<p class="center-button">
<button type="submit">Submit</button>
</p>
</form>

Q) When submitting the form, the form is invalid, when I print the f_formset and e_formset I get:

  • (Hidden field TOTAL_FORMS) This field is required.
  • (Hidden field INITIAL_FORMS) This field is required.
  • input type="hidden" name="dailyfood_set-TOTAL_FORMS" id="id_dailyfood_set-TOTAL_FORMS"
    input type="hidden" name="dailyfood_set-INITIAL_FORMS" id="id_dailyfood_set-INITIAL_FORMS"
    input type="hidden" name="dailyfood_set-MIN_NUM_FORMS" id="id_dailyfood_set-MIN_NUM_FORMS"
    input type="hidden" name="dailyfood_set-MAX_NUM_FORMS" id="id_dailyfood_set-MAX_NUM_FORMS"

    为什么这些值不存在,我在另一个项目中使用了 inlineformset 并且这些值由 django 自动填充,但由于某种原因它不会发生在一个项目中?

    最佳答案

    当您手动呈现表单集时,您需要包含带有标签 {{ your_formset.management_form }} 的 ManagementForm。表单集使用此表单来管理表单集中包含的表单集合。请参阅文档 herehere .

    <form action="" method="POST">
    {% csrf_token %}
    <div class="foods">
    <p class="ad-accordion">Calories Consumed</p>
    <div class="ad-accordion-content">
    {{ f_formset.management_form }}
    {% for form in f_formset %}
    {{form}}
    <br>
    {% endfor %}
    </div>
    </div>
    <div class="exercises">
    <p class="ad-accordion">Calories Burnt</p>
    <div class="ad-accordion-content">
    {{ e_formset.management_form }}
    {% for form in e_formset %}
    {{form}}
    <br>
    {% endfor %}
    </div>
    </div>
    <p class="center-button">
    <button type="submit">Submit</button>
    </p>
    </form>

    关于python - Django 内联表单集不会这样做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67235231/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com