tags:

views:

45

answers:

4

All,

I have a template page say x.html

i have 3 text fields name(varchar2) ,age(int),school(varchar2) in it.

If the users enters values in the form in x.html(say values name="a" ,age="2" ,school="a") and submit it.I need to return the same values back to x.html indicating an error.

My question is how to return the same values to x.html.

Thanks.....

+2  A: 

from docs:

The standard pattern for processing a form in a view looks like this:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })
Antony Hatchkins
Thanks...........................
Hulk
A: 

If you are using django forms, it would do validation itself and then return the values you need. Here you can read about using forms and how they validate values. Basically, when you pass some values into the form and it's not valid, you just render the site again, but django will automagically fill fields. Don't bother writing your own forms, unless you really, really need them. And in your example you really don't ;-)

gruszczy
Thanks....................................
Hulk
A: 

I'm not sure how you are processing your form information. However if you use the Form API built into Django, it takes care of much of this for you. For details take a look at the Django Docs for Forms http://docs.djangoproject.com/en/dev/topics/forms/#topics-forms-index

If you use the Form API and the submission is not valid, Django provides the template with a bound copy of the form with the user supplied data already in it. Again you will have to read the details of the API for how to implement it in your situation.

Steven Potter
Thanks...........................
Hulk
A: 

Django will write the submitted values back as long as you provide the form object to the rendered template. For example, in your view, something like:

# handle POST
form = MyForm(request.POST)
if form.is_valid():
    # do something and redirect
else:
    # render the template with the invalid form
    return render_to_response('mytemplate.html', {'form': form})

and in your template, something like:

{{ form.myfield.label_tag }}
{% if form.myfield.errors %} indicate error message/icon here {% endif %}
{{ form.myfield }}

Note that {{ form.myfield }} will show an HTML widget for myfield with the previous submitted values based on the view code above. And it will be blank when you render it with a blank form in response to a GET (e.g. form = MyForm()).

ars
Thanks......................................
Hulk
You're welcome. :)
ars