0

I'm a little confused why is 'clickjacking middleware` trowing Attribute Error on my form.

I'm making a simple application for collecting labor or user information, and I'm facing a small problem, can someone please help me and clarify what is wrong in this code

Dpaste from my Traceback, this is my view

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))

Urls

"""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')),
]
copser
  • 2,523
  • 5
  • 38
  • 73

1 Answers1

1

Your traceback is showing that you haven't used the view above at all, but the form. Presumably you've assigned the wrong thing in urls.py.

Edit Actually the problem is that your post method, when the form is not valid, returns the form itself and not an HttpResponse.

However you should not be defining any of these methods. You are just replicating what the class-based views are already supposed to be doing for you. Make your view actually inherit from CreateView and remove all those method definitions completely.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895