Django:POST 和 GET 请求并在模板上呈现
Django: POST and GET request and rendering on a template
我可能迷失在一杯水中,但此刻我想不通。我正在从事一个餐厅顶点项目,客户可以在该项目中看到菜单页面、购买页面,并且餐厅所有者在登录后能够管理和输入新食谱并创建他的个人菜单。我想做的是:当餐厅老板提交 POST 请求时,他输入了菜谱,我希望菜谱也出现在菜单所在的页面中。通过这种方式能够更新新配方并可能更改旧配方。 (我复制了模型、表单和查看代码以获得完整的概述):
form.py
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
fields = '__all__'
model.py
class Recipe(models.Model):
name = models.CharField(max_length=50)
ingredients = models.CharField(max_length=500)
def __str__(self):
return self.name
View.py
def recipeEntry(request):
recipe_menu = Recipe.objects.all()
form = RecipeForm()
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
form.save()
return redirect("recipe")
context = {'form':form, 'recipe_menu':recipe_menu}
return render(request, 'inventory/recipe.html', context)
recipe.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Recipe</title>
</head>
<body>
<form method="post", action="">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Add Recipe">
</form>
{% for rec in recipe_menu %}
<div>
<p>Recipe: {{rec.name}}</p>
<p>Ingredient :{{rec.ingredients}}</p>
</div>
{% endfor %}
</body>
</html>
目前提交 POST 请求的部分有效,所以只有第二部分无效。我尝试了一些解决方案,但我不知道该怎么做。我还想为菜单页面创建一个 GET 视图,但我需要传递一个 URL 来获取数据,但我没有用。
非常感谢您的帮助。
当它不是 post 请求时,您必须尝试显式实例化空表单:
def recipeEntry(request):
recipe_menu = Recipe.objects.all()
# form = RecipeForm() Not here yet
if request.method == 'POST':
# Instantiate form with request.POST
form = RecipeForm(request.POST)
if form.is_valid():
form.save()
return redirect("recipe")
else: # Explicitly write else block
# Instantiate empty form for get request
form = RecipeForm()
context = {'form':form, 'recipe_menu':recipe_menu}
return render(request, 'inventory/recipe.html', context)
我可能迷失在一杯水中,但此刻我想不通。我正在从事一个餐厅顶点项目,客户可以在该项目中看到菜单页面、购买页面,并且餐厅所有者在登录后能够管理和输入新食谱并创建他的个人菜单。我想做的是:当餐厅老板提交 POST 请求时,他输入了菜谱,我希望菜谱也出现在菜单所在的页面中。通过这种方式能够更新新配方并可能更改旧配方。 (我复制了模型、表单和查看代码以获得完整的概述):
form.py
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
fields = '__all__'
model.py
class Recipe(models.Model):
name = models.CharField(max_length=50)
ingredients = models.CharField(max_length=500)
def __str__(self):
return self.name
View.py
def recipeEntry(request):
recipe_menu = Recipe.objects.all()
form = RecipeForm()
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
form.save()
return redirect("recipe")
context = {'form':form, 'recipe_menu':recipe_menu}
return render(request, 'inventory/recipe.html', context)
recipe.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Recipe</title>
</head>
<body>
<form method="post", action="">{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Add Recipe">
</form>
{% for rec in recipe_menu %}
<div>
<p>Recipe: {{rec.name}}</p>
<p>Ingredient :{{rec.ingredients}}</p>
</div>
{% endfor %}
</body>
</html>
目前提交 POST 请求的部分有效,所以只有第二部分无效。我尝试了一些解决方案,但我不知道该怎么做。我还想为菜单页面创建一个 GET 视图,但我需要传递一个 URL 来获取数据,但我没有用。
非常感谢您的帮助。
当它不是 post 请求时,您必须尝试显式实例化空表单:
def recipeEntry(request):
recipe_menu = Recipe.objects.all()
# form = RecipeForm() Not here yet
if request.method == 'POST':
# Instantiate form with request.POST
form = RecipeForm(request.POST)
if form.is_valid():
form.save()
return redirect("recipe")
else: # Explicitly write else block
# Instantiate empty form for get request
form = RecipeForm()
context = {'form':form, 'recipe_menu':recipe_menu}
return render(request, 'inventory/recipe.html', context)