django form never valid?

This is the place for queries that don't fit in any of the other categories.

django form never valid?

Postby ole111 » Fri Apr 04, 2014 6:46 pm

Hello,
i am working on my first website, stuck at trying to read data from the page, the form is never valid.
That is, when I click 'submit' I always get 'not valid' message.
What do I do wrong? Thanx

views.py
Code: Select all
def home(request):
        if request.method == 'POST':
                localform = MajorForm(request.POST)
                if localform.is_valid():
                        return HttpResponse("valid")
                else:
                        return HttpResponse("not valid")
        return render(request,'index.html')


index.html
Code: Select all
        <form method="POST" style="margin-top:100px">
                {% csrf_token %}
                <p><textarea name="myarea" cols="50" rows="2"></textarea>
                <input type="submit" value="Send"></p>
        </form>



Code: Select all
class MajorForm(forms.Form):
        msg_text = forms.CharField(max_length=340,widget=forms.Textarea)
Last edited by Yoriz on Sun Apr 06, 2014 12:21 am, edited 1 time in total.
Reason: changed tittle.
ole111
 
Posts: 7
Joined: Thu Mar 27, 2014 11:23 pm

Re: form never valid

Postby micseydel » Sat Apr 05, 2014 12:29 am

What are you using for your server?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1393
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: form never valid

Postby ole111 » Sat Apr 05, 2014 11:38 pm

I an using the development server that comes with Django.
I
ole111
 
Posts: 7
Joined: Thu Mar 27, 2014 11:23 pm

Re: django form never valid?

Postby 7stud » Sun Apr 06, 2014 12:30 am

Form.errors
Access the errors attribute to get a dictionary of error messages:

>>> f.errors
{'sender': ['Enter a valid email address.'], 'subject': ['This field is required.']}
In this dictionary, the keys are the field names, and the values are lists of Unicode strings representing the error messages. The error messages are stored in lists because a field can have multiple error messages.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: django form never valid?

Postby ole111 » Sun Apr 06, 2014 4:24 pm

Ok, i got the idea in principle - but what is the way to access error attribute? From the command prompt? I mean, I launch the website, i can printout debug messages on the webpage. There must be a better way to debug, right?
ole111
 
Posts: 7
Joined: Thu Mar 27, 2014 11:23 pm

Re: django form never valid?

Postby 7stud » Sun Apr 06, 2014 7:31 pm

There must be a better way to debug, right?

Yes, it's called "writing tests":

https://docs.djangoproject.com/en/dev/topics/testing/
http://www.jeffknupp.com/blog/2013/12/1 ... right-way/
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am


Return to General Coding Help

Who is online

Users browsing this forum: Jacko333, stranac and 4 guests