Home > Software engineering >  Django "if request.method == 'POST':" returns False
Django "if request.method == 'POST':" returns False

Time:08-19

I'm making Django app and I have an issue, I've never had problem with before. As always in form view, I'm checking if request.method == 'POST' but somehow it returns False,

My code looks like that:

def recipe_create_view(request):
    context = {}

    form = RecipeForm(request.POST or None)
    IngredientFormset = formset_factory(IngredientForm)
    formset = IngredientFormset(request.POST or None)

    context['form'] = form
    context['formset']  = formset

    if request.method == 'POST':
        if form.is_valid():
            if formset.is_valid():
                form.save()
                print("made a recipe")
                
                for form in formset:
                    child = form.save(commit=False)
                    child.recipe = parent
                    child.save()
                    print("made a Ingredient")
            else:
                print("formset is not valid")
        else:
            print("form is not valid")
    else:
        print("request method is not correct")

    return render(request, 'recipes/create_recipe.html', context)

create_recipe.html file:

<form method="POST">
    {% csrf_token %}
    <label>recipe</label>
    <p>{{form}}</p>


    <label>ingredients</label>
    {% for form in formset %}
        
        <ul>
            <label>name</label>
            <li>{{ form.name }}</li>
            <label>quantity</label>
            <li>{{ form.quantity }}</li>
        </ul>
    {% endfor %}
    <div>
        <input type="submit" value="submit"  role="button">
    </div>
</form>

Where is the problem?

CodePudding user response:

It is necessary to return HttpResponseRedirect after dealing with POST data, the tip is not specific to Django, it's a good web practice in general.

Also, try to maintain both GET and POST request separately, so try below view:

def recipe_create_view(request):
    context = {}
    form="" # for the error of variable refrenced before assignment.
    IngredientFormset=""
    formset=""

    if request.method == 'POST':
        form = RecipeForm(request.POST)
        IngredientFormset = formset_factory(IngredientForm)
        formset = IngredientFormset(request.POST)

        if form.is_valid():
            if formset.is_valid():
                form.save()
                print("made a recipe")
                
                for form in formset:
                    child = form.save(commit=False)
                    child.recipe = parent
                    child.save()
                    print("made a Ingredient")
                return redirect('some_success_path_name')
   
            else:
                print("formset is not valid")
        else:
            print("form is not valid")
    else: # GET method
        print("request method is GET")
        form = RecipeForm()
        IngredientFormset = formset_factory(IngredientForm)
        formset = IngredientFormset()
        context['form'] = form
        context['formset']  = formset

        

    return render(request, 'recipes/create_recipe.html', context)

CodePudding user response:

add action in your HTML form and POST in small case.

<form action="/your_backend_url_to_view/" method="post">
    {% csrf_token %}
    <label>recipe</label>
    <p>{{form}}</p>


    <label>ingredients</label>
    {% for form in formset %}
        
        <ul>
            <label>name</label>
            <li>{{ form.name }}</li>
            <label>quantity</label>
            <li>{{ form.quantity }}</li>
        </ul>
    {% endfor %}
    <div>
        <input type="submit" value="submit"  role="button">
    </div>
</form>
  • Related