我从git中删除了一些无用的分支,现在我网站上的一个功能不起作用。它关于add_recipe不起作用。当我尝试添加它并且每个字段都已填写时,我按下“添加配方”按钮,它表明必须填写ingrediend字段。
查看:
def add_recipe(request):
add_recipe = RecipeForm(request.POST)
print(add_recipe['ingredients'].value())
if add_recipe.is_valid():
add_recipe.save()
return redirect('success_added_recipe')
return render(request, 'drinks/add_recipe.html', {'RecipeForm': add_recipe})
表格:
class RecipeForm(ModelForm):
class Meta:
model = Recipe
fields = ['recipe_name', 'preparation', 'ingredients', 'recipe_image']
debug.log
(0.000) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
(0.001) SELECT "drinks_ingredient"."id", "drinks_ingredient"."ingredient_name" FROM "drinks_ingredient" WHERE "drinks_ingredient"."id" IN (12); args=(12,)
(0.000) SELECT "drinks_ingredient"."id", "drinks_ingredient"."ingredient_name" FROM "drinks_ingredient"; args=()
"POST /accounts/add_recipe/ HTTP/1.1" 200 1637
(0.002)
型号:
class Recipe(models.Model):
recipe_name = models.CharField(max_length=250)
preparation = models.CharField(max_length=1000)
ingredients = models.ManyToManyField(Ingredient)
recipe_image = models.ImageField(upload_to='images/', default='')
def __str__(self):
return self.recipe_name
模板:
<h1>Add recipe</h1>
<form method="post" action="{% url 'add_recipe' %}">
{% csrf_token %}
<table>
{{RecipeForm}}
</table>
<input type="submit" value="Add recipe"/>
</form>
答案 0 :(得分:0)
只需在您的ManyToManyField中添加 blank = True
class Recipe(models.Model):
recipe_name = models.CharField(max_length=250)
preparation = models.CharField(max_length=1000)
ingredients = models.ManyToManyField(Ingredient,blank=True)
recipe_image = models.ImageField(upload_to='images/', default='')
def __str__(self):
return self.recipe_name