简体   繁体   English

Django视图无法识别请求

[英]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. 表单的目的是收集一些基本信息,然后将发送者的IP也保存到DB。 The form submits a POST request to my Django view, but Django gives me the error: 表单向我的Django视图提交POST请求,但是Django给了我错误:

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 这是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(); 我认为问题出在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. 请记住-这是python,在这里经常删除“ getter”以支持直接访问类成员。

So, try: 因此,请尝试:

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

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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