django “属性错误”:对象没有属性“get”

rlcwz9us  于 2022-12-14  发布在  Go
关注(0)|答案(2)|浏览(145)

错误:

AttributeError at / 'UserForm' object has no attribute 'get'

我无法调试这个错误!我忽略了我的代码很多次,但不能理解错误是在哪里产生的。请帮助我。提前感谢。

表单.py

class UserForm(forms.Form):
    username = forms.CharField()
    email = forms.EmailField()
    password1 = forms.PasswordInput()
    password2 = forms.PasswordInput()

    def clean(self):
        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        if password1 != password2:
            raise forms.ValidationError('Password Should Match')
        return self.cleaned_data

查看次数.py

def userview(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password1 = form.cleaned_data['password1']
            user = User(username=username,email=email,password =password1)
            user.save()
            return HttpResponse('User Created')
    else:
        form = UserForm()
    return render(request,'home.html',{'form':form})

模板主页.html

<form method="post" id="post-form">
    {% csrf_token %}
    {{form.as_p}}
<input type='submit' value ='ok'>
</form>

项目级别urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('useradd.urls'))
]

应用程序级别urls.py

from django.urls import path
from . import views
urlpatterns =[
    path('',views.UserForm,name = 'useradd')

]

全面追溯

Environment:

Request Method: GET
Request URL: http://localhost:8000/

Django Version: 2.0.5
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'useradd']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:

File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner 35.             
response = get_response(request)

File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\deprecation.py" in __call__ 97.             
response = self.process_response(request, response)

File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\django\middleware\clickjacking.py" in process_response 26.         
if response.get('X-Frame-Options') is not None:

Exception Type: AttributeError at /
Exception Value: 'UserForm' object has no attribute 'get'

按照指示,我已经提供了这个错误的完整追溯。我想我已经在views.py中定义了post方法,当该方法没有发布时,它将访问函数的else部分。但仍然不明白为什么会出现这样的错误。

mznpcxlj

mznpcxlj1#

问题在你的网址。而不是视图,你传递的形式到网址,它应该是这样的:

urlpatterns =[
    path('',views.userview,name = 'useradd')

]
zbq4xfa0

zbq4xfa02#

这是因为类UserForm没有get调用的方法或变量,需要先定义一个。

相关问题