Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:43:31+00:00 2026-06-09T18:43:31+00:00

I m new to django, I m writing a class-based view for a form.

  • 0

I m new to django, I m writing a class-based view for a form.
The problem is, this form renders well, but when posting I get form invalid.

I am not sure what should I do to get default validators validate the form. There are two fields in the form. One is choice field, whose choices are set dynamically based on logged-in user. Another is CharField. There is no need of validation as CharField is free text and ChoiceField is based on user’s data.

forms.py

class HomeForm(forms.Form):
    phones= forms.ChoiceField(label='Select phone', choices = (), required=True)
    message = forms.CharField(max_length=10, required=True)

    def __init__(self, user, *args, **kwargs):
        super(HomeForm, self).__init__(*args, **kwargs)
        print >> sys.stderr, 'form init'

        self.fields['phones'].choices = [(ids.id, ids.phone_model.short_name) for ids in GCM_RegId.objects.filter(user=user)]

    def process(self, user):
        cd = self.cleaned_data
        regid_id = cd['phones'].value
        gcm_regid = GCM_RegId.objects.filter(id=regid_id)

views.py

class HomeView(FormView):
    template_name = "home.html"
    form_class = HomeForm
    success_url = 'home'

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)

        if(form.is_bound):
            print >> sys.stderr, "Form is Bound"
        else:
            print >> sys.stderr, "Form is NOT Bound"

        if form.is_valid():
            print >> sys.stderr, "Form Valid"
            return self.form_valid(form)
        else:
            print >> sys.stderr, "Form NOT Valid"
            return self.form_invalid(form, **kwargs)       

    def get_form(self, form_class):
        form = HomeForm(self.request.user, {})
        return form

    def form_valid(self, form):
        form.process(self.request.user)
        print >> sys.stderr, "RegIdSubmitView form_valid"
        return super(HomeView, self).form_valid(form)   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:43:33+00:00Added an answer on June 9, 2026 at 6:43 pm

    I guess the problem is when you create the form instance, you forget to pass the request.POST data to your form class, the get_form method should be:

    def get_form(self, form_class):
        form = HomeForm(self.request.user, data=self.request.POST)
        return form
    

    Or, I think the better way for get_form is:

    def get_form(self, form_class):
        return form_class(self.request.user, **self.get_form_kwargs()
    

    Check the the source code of get_form_kwargs method in django.views.generic.edit.FormMixin, it automatically handle the request’s PUT/POST data and files for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I am studying the new django docs on class-based views, I notice this
I'm new with Django + Ajax. My Problem is I can't get the value
Alright, this is probably a really silly question but I am new to Python/Django
I create a new Django app (not project) called Bussinesses, then add following class
I'm new with Django and python. I have a problem. Is there a way,
I'm new to Django but am working on the tutorial on the Django website
I'm new to Django. I am writing my own administrative action for a third
I am relatively new with Django and it's ecosystem. I am writing REST api
I'm writing a Python-based shell script to boilerplate a Django app with virtualenv, pip,
I'm writing a Django app that performs various functions, including inserting, or updating new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.