0

I have a middleware like this:

class PostModelFormMiddleware(object):

def process_request(self, request):

    form = PostModelForm()
    request.post_form = form

This form has a captcha field. What I want to happen in my template is everytime a person hits Submit the captch question refreshes by Javascript to a new one. How do I grab that request.post_form and its data in jQuery?

Thank you!

EDIT: More code.

The HTML:

   <form id="add_post">
...
        <input type="hidden" name="math_captcha_question" value="fbc9fcdffbf2b2723ab36e9f341372214daf1be936202d2035">
    6 - 5 = 
...
    </form>

I would like to change the value of "math_captch_question" as well as the displayed text. Both of these are initially displayed using the template tag {{request.post_form.math_captcha_question}}

The jQuery:

 $("form#add_post").submit(function() {
 //Validations and stuff
            var data = {category:category, body:body, nickname:nickname, math_captcha_question:math_captcha_question, math_captcha_field:math_captcha_field};
            var args = {type:"POST", url:"add_post/", data:data, complete:function(res, status) {
                //Process response
                //This is where I would like to set new values for math_captcha_question

            } };

            $.ajax(args);
        }


        return false;

    })

/add_post/ view:

def add_post(request):
    if request.method == 'POST':

        if form.is_valid():
           #do stuff
           return HttpResponse("success")

        else:
            return HttpResponseServerError("fail")

1 Answer 1

1

You have to output the form in your template first, give it an id or other identifier and then you can access it via jQuery.

<form id="myform" ...>
    {{request.post_form}}
</form>

Edit: I am assuming you have 'django.core.context_processors.request' in your TEMPLATE_CONTEXT_PROCESSORS setting.

Update (according to comments below):

Your add_post view need to return something in the response jQuery.ajax can deal with. If it is just returning "success" there will be only plain text in the response. To make it easy you should return the new math_captcha_question value:

def add_post(request):
    if request.method == 'POST':
       #fill some data in the form
       form = PostModelForm(request.POST)
       if request.post_form.is_valid()
            math_captcha_question = form.cleaned_data["math_captcha_question"]
            return HttpResponse(math_captcha_question)
       else:
            return HttpResponse("error")

In the callback in jquery you can then deal with the values you get and give a user friendly response accordingly. In you on complete callback:

function(res, status) {
    if (res!="error") {
        $("form#add_post #id_math_captcha_question").attr("value", res);
    } else {
        //show some error to your user
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, Thanks for your reply. Yeah I already have it in the template. What I don't know how to do is to grab the new form that the middleware spits out through jQuery. I know how to replace old value with new value through jQuery. This new value is floating... somewhere... I tried to figure it out through Firebug, but I couldn't find it. Ideas?
Hm, can't really help without some code snippet from your side (the jQuery part you want to replace the value for a field in the form).
My guess is that you get the new value for math_captcha_question from the response of the POST request? Before you use jQuery I would suggest you to add an id or class to the math_captcha_question input. Assuming the id is "id_math_captcha_question" you should then be able to do something like $("form#add_post #id_math_captcha_question").attr("value", new_value_from_post_response); in the lines you mentioned in you JS.
Ok, then one last code addition I think: How does your view handling the post request to the url "add_post/" look like? Btw. "add_post/" looks like a relative URL. Maybe its better to use absolute URL's kike "/add_post/", simply to avoid URL dependency issues.
About context processors check docs.djangoproject.com/en/dev/ref/templates/api/…. Its way easier then dealing with middleware's. Each view which is rendered with context_instance = RequestContext(request) will then use the context_processors provided in TEMPLATE_CONTEXT_PROCESSORS setting`. If you are not attempting to change request or response objects and you just need a variable in all views which render templates then choose context processors over middleware.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.