Home > Net >  Django form is not saving when I have action = to desired url
Django form is not saving when I have action = to desired url

Time:11-23

If I use action="" in my django form, the form works properly but sends the user to the wrong page. I want the user to go back to the macro/ page upon form submission, but when I add that url to action (like action="{% url 'macro' %}", it goes to the page but the form doesn't save. Any suggestion on how to handle this? Code below:

(Option 1) macro_update.html -> the form here works properly, but takes the user to the wrong page

                <ul>
                    <form action="" method="post">
                        {% csrf_token %}
                        {{ macro_form.as_ul }}
                        <input type="submit" value="Submit">
                    </form>
                </ul>

(Option 2) macro_update.html -> the user is redirected to the right page upon submission, but the form data doesn't update/save

                <ul>
                    <form action="{% url 'macro' %}" method="post">
                        {% csrf_token %}
                        {{ macro_form.as_ul }}
                        <input type="submit" value="Submit">
                    </form>
                </ul>

views.py

@login_required(login_url='login')
def macroUpdate(request):

    if request.method == "POST":
        macro_form = MacroForm(request.POST, instance=request.user.profile)
        if macro_form.is_valid():
            macro_form.save()
            messages.success(request,('Your macros were successfully updated!'))
        else:
            messages.error(request,('Unable to complete request'))
        return redirect("profile")

    macro_form = MacroForm(instance=request.user.profile)    
    context = {"user":request.user, "macro_form":macro_form } 
    return render(request, 'macro_update.html', context)

urls.py

urlpatterns = [
    path('', views.loginPage, name='login'),
    path('register/', views.registerPage, name='register'),
    path('profile/', views.profilePage, name='profile'),
    path('profile/update/', views.profileUpdate, name='profile-update'),
    path('logout/', views.logoutUser, name='logout-user'),
    path('macro/', views.macroPage, name='macro'),
    path('macro/update/', views.macroUpdate, name='macro-update'),
]

CodePudding user response:

I want the user to go back to the macro/ page upon form submission, but when I add that url to action (like action="{% url 'macro' %}", it goes to the page but the form doesn't save.

It is because form data must go to macroUpdate view to save not macroPage, to redirect on macro page after form submission you can use redirect("macro") so:

views.py:

@login_required(login_url='login')
def macroUpdate(request):

    if request.method == "POST":
        macro_form = MacroForm(request.POST, instance=request.user.profile)
        if macro_form.is_valid():
            macro_form.save()
            messages.success(request,('Your macros were successfully updated!'))
        else:
            messages.error(request,('Unable to complete request'))
        return redirect("macro")

    macro_form = MacroForm(instance=request.user.profile)    
    context = {"user":request.user, "macro_form":macro_form } 
    return render(request, 'macro_update.html', context)

Just remove the action attribute in from tag of macro_update.html since Django always takes current page route so:

<ul>
    <form method="POST">
        {% csrf_token %}
        {{ macro_form.as_ul }}
        <input type="submit" value="Submit">
    </form>
</ul>
  • Related