简体   繁体   中英

Django view not recognizing request

I have a form that is filled out on a webpage. The goal of the form is to gather some basic info, and then save the IP of the sender to the DB too. The form submits a POST request to my Django view, but Django gives me the error:

if request.method() == 'POST':
TypeError: 'str' object is not callable

Here is the view:

from .form import SignUpForm
from django.shortcuts import render
from django.http import HttpResponseRedirect


def index(request):

    if request.method() == 'POST':
         form = SignUpForm(request.POST)
         if form.is.valid():
             signup_item = form.save(commit=False)
             signup_item.ip_address = request.META['HTTP_X_FORWARDED_FOR']
             signup_item.save()
             return HttpResponseRedirect(request.path)
    else:
        form = SignUpForm()
    return render(request, 'index.html', {'form': form})

Here is the urls.py

 from django.conf.urls.import url
 from django.contrib import admin
 from form import views

 urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url(r'', views.index, name='home')
 ]

I think the issue is with request.method(); method is a string data member, not a member function. Remember - this is python, where 'getters' are often dropped in favor of directly accessing class members.

So, try:

if request.method == 'POST':
    form = SignUpForm(request.POST)

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM