On 17 December 2010 19:46, hank23 <hversem...@stchas.edu> wrote:
> Thanks for the note. Below is the screen code again which I've changed
> a little, plus the two views which deal with it. Let me know if you
> need anything else. Here's the screen code:
>
> <h1>Add Poll Question Screen</h1>
>
> {% if error_message %}<p><strong>{{ error_message }}</strong></p>{%
> endif %}
>
> <form action="/polls/addpoll/" method = "post">{% csrf_token %}
>
> <strong>QUESTION: </strong>
>    <input nsme="question" type=text size=75 maxlength=70
> value="please enter poll question text here">
> <br /><br />
> <input type="submit" value="addpoll" />
>
> </form>
>
> Here's the code to display the screen initially:
>
> @csrf_protect
> def addnewpoll(request):
>    p = get_list_or_404(Poll)
>    dctnry = {}
>    pollcount = 0
>    for item in p:
>        pollcount = pollcount + 1
>    dctnry['pcount'] = pollcount
>    return render_to_response('polls/addpoll.html', dctnry,
>
> context_instance=RequestContext(request))
>
> Here's the code of the view to receive the request after the addpoll
> button on the screen is clicked:
>
> @csrf_protect
> def addpoll(request):
>    dctnry = {}
>    dctnry['error_message'] = "No new poll data added"
>    return render_to_response('polls/addpoll.html', dctnry,
>
> context_instance=RequestContext(request))
>
> This last block of code I just wanted to display an error messge on
> the screen the first time so I knew itwas receiving the request ok.
> How would I extract the question text from the textbox on the screen/
> request if I wanted to in this view, so that I could take it an create
> a new poll object? The documentation on django is good but sometimes
> pieces of information seem to be missing so that its difficult to
> figure how everything works. More examples with complete coding and
> more detailed explanations would help in some instances. Thanks in
> advance for the help.
>

This sort of user interaction is usually best done with Django forms. Look 
at the documentation for standard forms here:
http://docs.djangoproject.com/en/1.2/topics/forms/
The forms framework will handle form creation, display and validation. 
There's also a specific ModelForm class, which abstracts form creation based 
on a model and creating model instances based on the POSTed values, see 
here:
http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/

Note specifically the "Using a form in a view" section of that first link, 
which shows the standard pattern of handling both the initial form request 
and the processing of the submitted data in the same view, rather than 
separate views for each. Also, you shouldn't need to specifically decorate 
your views with @csrf_protect: since version 1.2, CSRF protection is always 
on by default.

To answer your specific question on how to get access to POSTed values, you 
can always use request.POST['fieldname'] - see the request documentation 
here:
http://docs.djangoproject.com/en/1.2/ref/request-response/
But as I say above, you're better off using the forms framework in this 
situation.

One final point: your code above to get the count of Polls in your database 
is very inefficient. You can do in one go:
    Poll.objects.all().count()
which simply sends a SELECT COUNT(*) to the database. 
--
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-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to