Re: Redirecting within custom template tags
On Jul 6, 9:57 am, neebone <[EMAIL PROTECTED]> wrote: > On Jul 6, 9:46 am, Malcolm Tredinnick <[EMAIL PROTECTED]> > wrote: > > > > > On Fri, 2007-07-06 at 08:26 +, neebone wrote: > > > [...] > > > > Ok, I've got a page which uses a generic view to load the article into > > > the template. All good so far. I've then created two template tags, > > > list_comments and comment_form. > > > list_comments is displayed first. The tag grabs the comments > > > associated with article and returns a rendered template. > > > comment_form does the same expect returns a rendered form. Now, > > > because the article page sends the request context to its template, > > > all the tags within the template can read the request variable too. > > > This means, the form tag can also detect if a POST action has taken > > > place, and add a comment (which is how it works). > > > > So this poses the problem - am I using the tag for the wrong thing? I > > > dont want to send the post request off to a different url which will > > > handle adding the commenthowever I could, instead of using a > > > generic view, use a custom article view to detect a post request and > > > load a different view to add the comment - redirecting back to article > > > which solves my problem. > > > This is a very unusual way of coding, I suspect. You seem to be trying > > to put the logic that would normally be part of a view (which is > > basically the business logic and request handling side of an app) into > > the template rendering phase instead (presentation logic). So, yes, > > there are going to be some request handling things you can't do, at > > least not easily. > > > If I were you, I would just use normal views to do this. I can't see > > that you are saving yourself any code doing it the current way -- you > > still have to write all the POST processing code, but you've put it in > > the tag instead of another function. > > > Certainly trying to do any acting on the request using redirection or > > anything like that is inappropriate for the rendering phase. You're > > already into the "produce output" phase by that point and have gone past > > the point of controlling where the output goes. > > > Regards, > > Malcolm > > > -- > > I don't have a solution, but I admire your > > problem.http://www.pointy-stick.com/blog/ > > Yeh, I see what your saying. I was trying to emulate, to a degree, how > the django comment stuff works (without actually looking at the code). > The tag itself is basic - it just calls a comment function which > handles adding the comment. > > I wanted the comments to be dropped into any page (using generic > relations to any given model). If anything, the main reason I needed > the redirect was because the comments get listed first, then the form > gets processed. But obviously when the new comment has been added, the > list has already been rendered - meaning the new comment wont appear > until the page is refreshed! > > Ok, back to the drawing board... Thanks Malcom. Just to update, I'm getting the form to post to a different url which handles the logic of creating the comment then redirects back to the article page on success. All good. My only problem is what to do if the form contains missing fields - I need to get back to the article page and re-populate the form with the entered information _plus_ error messages (field required etc). Any way to send a POST request from a view to a given url similar to the way Client works in django.test.client? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Redirecting within custom template tags
On Jul 6, 9:46 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Fri, 2007-07-06 at 08:26 +, neebone wrote: > > [...] > > > > > Ok, I've got a page which uses a generic view to load the article into > > the template. All good so far. I've then created two template tags, > > list_comments and comment_form. > > list_comments is displayed first. The tag grabs the comments > > associated with article and returns a rendered template. > > comment_form does the same expect returns a rendered form. Now, > > because the article page sends the request context to its template, > > all the tags within the template can read the request variable too. > > This means, the form tag can also detect if a POST action has taken > > place, and add a comment (which is how it works). > > > So this poses the problem - am I using the tag for the wrong thing? I > > dont want to send the post request off to a different url which will > > handle adding the commenthowever I could, instead of using a > > generic view, use a custom article view to detect a post request and > > load a different view to add the comment - redirecting back to article > > which solves my problem. > > This is a very unusual way of coding, I suspect. You seem to be trying > to put the logic that would normally be part of a view (which is > basically the business logic and request handling side of an app) into > the template rendering phase instead (presentation logic). So, yes, > there are going to be some request handling things you can't do, at > least not easily. > > If I were you, I would just use normal views to do this. I can't see > that you are saving yourself any code doing it the current way -- you > still have to write all the POST processing code, but you've put it in > the tag instead of another function. > > Certainly trying to do any acting on the request using redirection or > anything like that is inappropriate for the rendering phase. You're > already into the "produce output" phase by that point and have gone past > the point of controlling where the output goes. > > Regards, > Malcolm > > -- > I don't have a solution, but I admire your > problem.http://www.pointy-stick.com/blog/ Yeh, I see what your saying. I was trying to emulate, to a degree, how the django comment stuff works (without actually looking at the code). The tag itself is basic - it just calls a comment function which handles adding the comment. I wanted the comments to be dropped into any page (using generic relations to any given model). If anything, the main reason I needed the redirect was because the comments get listed first, then the form gets processed. But obviously when the new comment has been added, the list has already been rendered - meaning the new comment wont appear until the page is refreshed! Ok, back to the drawing board... Thanks Malcom. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Redirecting within custom template tags
On Jul 6, 9:13 am, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > On Fri, 2007-07-06 at 08:04 +, neebone wrote: > > Hi, > > > I'm trying to roll my own comments custom template tag and need to > > action a browser redirect on successful comment posting. My problem is > > that you can't just return HttpResponseRedirect and expect it to be > > executed. Is there an "official" way to get the middleware to action > > the redirect from inside the tag? > > I think you're confusing a few unrelated things here. Template tags are > just a way to produce strings which are inserted into the output. They > are completely unaware of HTTP or anything like that. > > If you want the browser to redirect somewhere after the form is posted, > that needs to be done by the view that handles the form posting. Now, > you might want to include where to redirect to in the form itself, which > is information your template tag could include as a hidden form element > (for example), but, again, the tag is just producing a string that is > included verbatim. > > One final note: if you do include the final target in the form, remember > to verify it is correct before the subsequent view acts on it, since the > value can be changed by anybody submitting the form. Encrypting the > target value or including a hash of the target with a secret key are a > couple of approaches there. > > Regards, > Malcolm > > -- > Honk if you love peace and quiet.http://www.pointy-stick.com/blog/ Thanks for quick reply. Ok, let me explain in a little more detail Ok, I've got a page which uses a generic view to load the article into the template. All good so far. I've then created two template tags, list_comments and comment_form. list_comments is displayed first. The tag grabs the comments associated with article and returns a rendered template. comment_form does the same expect returns a rendered form. Now, because the article page sends the request context to its template, all the tags within the template can read the request variable too. This means, the form tag can also detect if a POST action has taken place, and add a comment (which is how it works). So this poses the problem - am I using the tag for the wrong thing? I dont want to send the post request off to a different url which will handle adding the commenthowever I could, instead of using a generic view, use a custom article view to detect a post request and load a different view to add the comment - redirecting back to article which solves my problem. BUT! That means my comment code isnt self contained within the tags I hope that made sense. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Redirecting within custom template tags
Hi, I'm trying to roll my own comments custom template tag and need to action a browser redirect on successful comment posting. My problem is that you can't just return HttpResponseRedirect and expect it to be executed. Is there an "official" way to get the middleware to action the redirect from inside the tag? FYI, I realised this bug because I have a separate tag which displays comments BEFORE the form tag gets loaded (and hence only adds the new comment AFTER all the existing comments get rendered by the previous tag in the template...doh!) Cheers guys/gals --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---