[Answered ]-Django: POST and GET request and rendering on a template

1👍

You must try with explicitly instantiate empty form when it’s not a post request :

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)

Leave a comment