AttributeError:“ tuple”对象在views.py

时间:2018-12-24 10:14:31

标签: django django-forms django-views

我想创建一个注册表,但是在运行应用程序时出现属性错误

from django.shortcuts import render
from basic_app.forms import UserForm,UserProfileInfoForm


def index(request):
    return render(request,'basic_app/index.html')
def register(request):
    registered=False
    if request.method=="POST":
        user_form=UserForm(data=request.POST)
        profile_form=UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_from.is_valid():
            user=user_form.save()
            user.setpassword(user.password)
            user.save()

            profile=profile_form.save(commit=False)
            profile.user=user

            if 'profile_pic' in request.FILES:
                profile.profile_pic=request.FILES['profile_pic']
            profile.save()

            registered=True

        else:
        print(user_form.errors,profile_form.errors)
    else:
        user_form=UserForm()
        profile_form=UserProfileInfoForm()
    return(request,'basic_app/registration.html',

          {'user_form':user_form,
            'profile_form':profile_form,
            'registered':registered})

输出

内部服务器错误:/ basic_app / register / 追溯(最近一次通话):   文件“ C:\ Users \ Shoaib Khan \ AppData \ Local \ conda \ conda \ envs \ myenv \ lib \ site-packages \ django \ core \ handlers \ exception.py”,第34行,位于内部     响应= get_response(请求)   文件“ C:\ Users \ Shoaib Khan \ AppData \ Local \ conda \ conda \ envs \ myenv \ lib \ site-packages \ django \ utils \ deprecation.py”,调用中的第93行     响应= self.process_response(请求,响应)   文件“ C:\ Users \ Shoaib Khan \ AppData \ Local \ conda \ conda \ envs \ myenv \ lib \ site-packages \ django \ middleware \ clickjacking.py”,第26行,位于process_response

if response.get('X-Frame-Options') is not None:

AttributeError:“元组”对象没有属性“ get” [24 / Dec / 2018 15:34:51]“ GET / basic_app / register / HTTP / 1.1” 500 61448

2 个答案:

答案 0 :(得分:2)

那是因为这一行:

return(request,'basic_app/registration.html',

      {'user_form':user_form,
        'profile_form':profile_form,
        'registered':registered})

您实际上已经在这里创建了一个元组。请注意您要返回的三件事的括号?

这是在django中渲染模板的方式:

from django.shortcuts import render

render(request, 'polls/index.html', context)

因此,在您的情况下,它将起作用:

render(request,'basic_app/registration.html', {
                                         'user_form':user_form,
                                         'profile_form':profile_form,
                                         'registered':registered
                                      })

有关render的更多信息,请查看其docs

答案 1 :(得分:1)

更改此

return(request,'basic_app/registration.html',

          {'user_form':user_form,
            'profile_form':profile_form,
            'registered':registered})

return render(request,'basic_app/registration.html',

          {'user_form':user_form,
            'profile_form':profile_form,
            'registered':registered})