PersonalInfoForm 的属性错误
Attribute error for PersonalInfoForm
我有点困惑为什么我的表单上的“clickjacking 中间件”trowing 属性错误。
我正在制作一个简单的应用程序来收集人工或用户信息,我遇到了一个小问题,有人可以帮助我并澄清这段代码中的错误吗
Dpaste 来自我的 Traceback,这是我的观点
class PersonalInfoView(FormView):
"""TODO: CreateView for PersonalInfoForm
return: TODO
"""
template_name = 'apply_to/apply_now.html'
form_class = PersonalInfoForm
success_url = 'success/'
def get(self, form, *args, **kwargs):
"""TODO: define get request
return: TODO
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(
self.get_context_data(form=form))
def post(self, form, *args, **kwargs):
"""TODO: Post request for PersonalInfoForm
return: TODO
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_class(form)
def form_valid(self, form, *args, **kwargs):
"""TODO: Validate form
return: TODO
"""
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, *args, **kwargs):
"""TODO: handle invalid form request
return: TODO
"""
return self.render_to_response(
self.get_context_data(form=form))
网址
"""superjobs URL Configuration
the `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
examples:
function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
including another URLconf
1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView
from labor_apply_app.views import PersonalInfoView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
# django-contrib-flatpages
# url(r'^apply_to/', include('labor_apply_app.urls')),
url(r'^$', 'labor_apply_app.views.index', name='index'),
url(r'^apply_now/$', PersonalInfoView.as_view()),
url(r'^success/$', TemplateView.as_view()),
# Django Allauth
url(r'^accounts/', include('allauth.urls')),
]
您的回溯表明您根本没有使用上面的视图,而是使用了表单。大概你在 urls.py.
中分配了错误的东西
编辑 实际上,问题在于您的 post 方法,当表单无效时,returns 表单本身而不是 HttpResponse。
但是您不应该定义这些方法中的任何一个。您只是在复制基于 class 的视图应该为您做的事情。让您的视图真正继承自 CreateView 并完全删除所有这些方法定义。
我有点困惑为什么我的表单上的“clickjacking 中间件”trowing 属性错误。
我正在制作一个简单的应用程序来收集人工或用户信息,我遇到了一个小问题,有人可以帮助我并澄清这段代码中的错误吗
Dpaste 来自我的 Traceback,这是我的观点
class PersonalInfoView(FormView):
"""TODO: CreateView for PersonalInfoForm
return: TODO
"""
template_name = 'apply_to/apply_now.html'
form_class = PersonalInfoForm
success_url = 'success/'
def get(self, form, *args, **kwargs):
"""TODO: define get request
return: TODO
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(
self.get_context_data(form=form))
def post(self, form, *args, **kwargs):
"""TODO: Post request for PersonalInfoForm
return: TODO
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_class(form)
def form_valid(self, form, *args, **kwargs):
"""TODO: Validate form
return: TODO
"""
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, *args, **kwargs):
"""TODO: handle invalid form request
return: TODO
"""
return self.render_to_response(
self.get_context_data(form=form))
网址
"""superjobs URL Configuration
the `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
examples:
function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
including another URLconf
1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView
from labor_apply_app.views import PersonalInfoView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
# django-contrib-flatpages
# url(r'^apply_to/', include('labor_apply_app.urls')),
url(r'^$', 'labor_apply_app.views.index', name='index'),
url(r'^apply_now/$', PersonalInfoView.as_view()),
url(r'^success/$', TemplateView.as_view()),
# Django Allauth
url(r'^accounts/', include('allauth.urls')),
]
您的回溯表明您根本没有使用上面的视图,而是使用了表单。大概你在 urls.py.
中分配了错误的东西编辑 实际上,问题在于您的 post 方法,当表单无效时,returns 表单本身而不是 HttpResponse。
但是您不应该定义这些方法中的任何一个。您只是在复制基于 class 的视图应该为您做的事情。让您的视图真正继承自 CreateView 并完全删除所有这些方法定义。