Django:“request”不包含'method'(POST / GET)

时间:2016-08-09 08:32:36

标签: python django django-forms django-views

我为客户创建了一个[貌似]直截了当的表单,但是,在用户提交表单后,当我尝试检查返回的请求时,我得到了:

例外类型: AttributeError的 例外价值: 'QueryDict'对象没有属性'method'

当我检查时,我看到请求对象只包含表单中提交的数据,并且不包含“method”属性

这是表单使用的函数:

def institutionapply(request):
    print(request)
    print("request: ",request)
    a=get_ip(request)
    print (a)
    country=get_country(request)
    # if this is a POST request we need to process the form data

    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = institutionapply(request.POST)
        print ("form in post clause:",form)
        #print ("duration output:",form.duration)
        # check whether it's valid:
        if form.is_valid():
            print ("print form is valid")
            print (form.cleaned_data['title'])
            print (form.cleaned_data['country'])

            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            #return HttpResponseRedirect('/thanks/')
            #return redirect(name='home')
    # if a GET (or any other method) we'll create a blank form
    else:
        # create a form instance and populate it with data from the request:
        data = {'title': '', 'first': '', 'last':'', 'country':'','internationalprefix':'','phone':'','email':'','institutionname':'','institutionurl':'',}
        form = institutionform(data)
    return render(request,'app/intitutionapply.html', {'form': form})

为什么无法找到“方法”对象的任何想法?

编辑:网址配置:

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^InstitutionApply$', views.institutionapply, name='institutionapply'),
    url(r'^contact$', views.contact, name='contact'),
    url(r'^about', views.about, name='about'),
    url(r'^admin/', admin.site.urls),

从html模板中,表单声明为:

来自views.py的功能

def get_country(request):
    a=get_ip(request)
    country='United States'
    g = GeoIP2(path='D:/newTelumis/newTelumis/GeoLite2-Country.mmdb')
    return country



def  get_ip(request):
    try:
        x_forward= request.META.get("HTTP_X_FORWARD_FOR")
        if x_forward:
            ip=x_forward.split(",")[0]
        else:
            ip=request.META.get("REMOTE_ADDR")
    except:
        ip=""
    return ip

1 个答案:

答案 0 :(得分:3)

def institutionapply(request):
    ...
    if request.method == 'POST':
        ...
        form = institutionapply(request.POST)

institutionapply替换为institutionform