Code

Changes between Version 10 and Version 11 of UsingFreeComment


Ignore:
Timestamp:
12/20/06 11:52:23 (7 years ago)
Author:
Nicolas Lara
Comment:

Free comments without preview

Legend:

Unmodified
Added
Removed
Modified
  • UsingFreeComment

    v10 v11  
    167167}}} 
    168168 
     169In this Example change name="preview" to name="post" if you dont want to be redirected to a preview page 
     170 
    169171=== Preview (free_preview.html) === 
    170172 
     
    233235{% endif %} 
    234236}}} 
     237 
     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 views.py 
     241{{{ 
     242#!python 
     243from django.contrib.comments.views.comments import post_free_comment 
     244from django.http import HttpResponseRedirect 
     245 
     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) 
     253}}} 
     254 
     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 urls.py 
     257{{{ 
     258#!python 
     259(r'^comments/post/', 'views.my_post_free_comment'), 
     260}}} 
     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 settings.py 
     262{{{ 
     263#!python 
     264from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS 
     265TEMPLATE_CONTEXT_PROCESSORS += ( 
     266     'django.core.context_processors.request', 
     267)  
     268}}} 
     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: 
     270{{{ 
     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 %} 
     282}}} 
     283 
     284This should be it. Enjoy 
     285  --NL 
    235286 
    236287== Other Examples ==