Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Django request.method is 'GET' not 'POST'?

I met this strange problem when doing a pre-populated form. In my template, the form method is clearly stated as POST:

<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">{% csrf_token %}

But in my view function, the request.method turns out to be GET.

Below is my view function:

def editProfile(request,template_name):
    theprofile = request.user.profile

    print theprofile.fullname

    notificationMSG = ''
    print request.method

    if request.method == 'POST':
        form = UserProfileForm(request.POST,request.FILES, instance=theprofile)
        if form.is_valid():
            form.save()
            notificationMSG = "success!"

    else:
        form = UserProfileForm()
        print "error"

    dic = {'form':form,
           'notificationMSG':notificationMSG}

    return render_to_response(template_name, dic, context_instance=RequestContext(request))

When I run it, it prints out GET. Anyone met this odd before?

like image 657
Wei Xu Avatar asked Sep 17 '25 11:09

Wei Xu


1 Answers

In my case I was missing a "/" at the end of action in the HTML Template, while posting.

like image 187
py_ios_dev Avatar answered Sep 19 '25 01:09

py_ios_dev