Changes between Version 10 and Version 11 of UsingFreeComment

12/20/2006 01:52:23 PM (9 years ago)
Nicolas Lara

Free comments without preview


  • UsingFreeComment

    v10 v11  
     169In this Example change name="preview" to name="post" if you dont want to be redirected to a preview page
    169171=== Preview (free_preview.html) ===
    233235{% endif %}
     238== Wrapping post_free_comment to change the redirect url ==
     239At this time django.contrib.comments.views.comments has hard-coded, on its post_free_comment method, a redirection address to ''posted.html''. If you want your comments to redirect to the same page you can wrap the post_free_comment method and add a redirect to another after the comment creation. The url can be passed as a hidden parameter in freeform.html
     240To do this you first need to create a wrapper in
     243from django.contrib.comments.views.comments import post_free_comment
     244from django.http import HttpResponseRedirect
     246def my_post_free_comment(request):
     247    if request.POST.has_key('url'):
     248        url = request.POST['url']
     249    else:
     250        url = '/comments/posted.html'
     251    response = post_free_comment(request)
     252    return HttpResponseRedirect(url)
     255After you have your view you need to point the requests to that view instead of the default comments view
     256To do this add the following line to
     259(r'^comments/post/', 'views.my_post_free_comment'),
     261And change the form to include the url as a hidden field. The problem is: How do I get the current url in a template variable?. To do this you need to activate the  template context processor called ''request'' adding the following code to
     264from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
     266     'django.core.context_processors.request',
     269Once your request context processor is activated you will have the variable request available in your templates. With it you can get request.get_full_path to print the current url as follows:
     271{% if display_form %}
     272        <form action="/comments/post/" method="post">
     273                <p>Your name: <input type="text" id="id_person_name" name="person_name" /></p>
     274                <p>Comment:<br /><textarea name="comment" id="id_comment" rows="10" cols="60"></textarea></p>
     275                <input type="hidden" name="options" value="{{ options }}" />
     276                <input type="hidden" name="target" value="{{ target }}" />
     277                <input type="hidden" name="gonzo" value="{{ hash }}" />
     278                <input type="hidden" name="url" value="{{ request.get_full_path }}" />
     279                <p><input type="submit" name="post" value="Post" /></p>
     280        </form>
     281{% endif %}
     284This should be it. Enjoy
     285  --NL
    236287== Other Examples ==
Back to Top