Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 13, 2011 19:31:03

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Screen Errors


I have a screen which displays just fine the first time in, however
when I knowingly submit the page without entering data in all of the
fields to cause an error the page loses some of the data and controls.
I'm sure my view code is probably missing something just not sure at
this point what exactly is missing. So far my view is coded according
to the example given in the document athttp://docs.djangoprojects.com/en/1.2/topics/forms/.
The example though does not seem to clearly indicate what should be
sent back to the screen in the case of errors. Below is my addchoice
view which is the one in question:

def addchoice(request):
message = ""
pollcount = Poll.objects.all().count()

if pollcount > 0 :
message = "Number of polls passed to form was " +
str(pollcount)
else:
message = "No polls passed in to form."

dctnry = {}

if request.method == "POST":
form = AddChoiceForm(request.POST)
if form.is_valid():
poll = form.cleaned_data
slctdchoice = form.cleaned_data
if isinstance(poll.id, int):
newchoice = Choice(poll=poll.id, choice=slctdchoice,
votes=0)
newchoice.save()
confirmmessage = "Choice: " + slctdchoice + " : has
been save for poll: " + str(pollid) + "."
dctnry = { 'form': form, 'infomessage': message,
'confirmmessage': confirmmessage }

else:
form = AddChoiceForm()
dctnry = { 'form': form, 'infomessage': message }

return render_to_response('polls/addchoice.html',
dctnry,

context_instance=RequestContext(request))


I would appreciate any suggestions for how to correc the code or links
to one or more complete screen error processing view code. Thanks for
the help.



--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 14, 2011 12:27:29

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Screen Errors


On Thursday, January 13, 2011 7:30:56 PM UTC, hank23 wrote:
>
> I have a screen which displays just fine the first time in, however
> when I knowingly submit the page without entering data in all of the
> fields to cause an error the page loses some of the data and controls.
> I'm sure my view code is probably missing something just not sure at
> this point what exactly is missing. So far my view is coded according
> to the example given in the document at
>http://docs.djangoprojects.com/en/1.2/topics/forms/.
> The example though does not seem to clearly indicate what should be
> sent back to the screen in the case of errors. Below is my addchoice
> view which is the one in question:
>
> <snip>
>
> I would appreciate any suggestions for how to correc the code or links
> to one or more complete screen error processing view code. Thanks for
> the help.
>


You need to think about the path through the code that happens when
request.method is POST, but the form is not valid. In that case, in your
current code, dctnry is empty, so does not pass the form back to the
template.

Since you want to modify the contents of dctnry when the form is saved, I
would do it like this:

dctnry = {'infomessage': message}

if request.method == "POST":
form = AddChoiceForm(request.POST)
if form.is_valid():
poll = form.cleaned_data
slctdchoice = form.cleaned_data
if poll:
newchoice = Choice(poll=poll.id,
choice=slctdchoice, votes=0)
newchoice.save()
confirmmessage = "Choice: %s has been saved for poll: %s" %
(slctdchoice, pollid)
dctnry = confirmmessage

else:
form = AddChoiceForm()

dctnry = form
return render_to_response('polls/addchoice.html' dctnry,
context_instance=RequestContext(request))

which ensures that the form is always added to the context, no matter where
it is instantiated.
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 14, 2011 14:17:14

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django Screen Errors


Thanks for the help. I'll modify my code accordingly.

On Jan 14, 6:27 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Thursday, January 13, 2011 7:30:56 PM UTC, hank23 wrote:
>
> > I have a screen which displays just fine the first time in, however
> > when I knowingly submit the page without entering data in all of the
> > fields to cause an error the page loses some of the data and controls.
> > I'm sure my view code is probably missing something just not sure at
> > this point what exactly is missing. So far my view is coded according
> > to the example given in the document at
> >http://docs.djangoprojects.com/en/1.2/topics/forms/.
> > The example though does not seem to clearly indicate what should be
> > sent back to the screen in the case of errors. Below is my addchoice
> > view which is the one in question:
>
> > <snip>
>
> > I would appreciate any suggestions for how to correc the code or links
> > to one or more complete screen error processing view code. Thanks for
> > the help.
>
> You need to think about the path through the code that happens when
> request.method is POST, but the form is not valid. In that case, in your
> current code, dctnry is empty, so does not pass the form back to the
> template.
>
> Since you want to modify the contents of dctnry when the form is saved, I
> would do it like this:
>
>     dctnry = {'infomessage': message}
>
>     if request.method == "POST":
>         form = AddChoiceForm(request.POST)
>         if form.is_valid():
>             poll = form.cleaned_data
>             slctdchoice = form.cleaned_data
>             if poll:
>                 newchoice = Choice(poll=poll.id,
> choice=slctdchoice, votes=0)
>                 newchoice.save()
>                 confirmmessage = "Choice: %s has been saved for poll: %s" %
> (slctdchoice, pollid)
>                 dctnry = confirmmessage
>
>     else:
>         form = AddChoiceForm()
>
>     dctnry = form
>     return render_to_response('polls/addchoice.html' dctnry,
>                      context_instance=RequestContext(request))
>
> which ensures that the form is always added to the context, no matter where
> it is instantiated.
> --
> DR.- Hide quoted text -
>
> - Show quoted text -

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support