Code

Changes between Version 29 and Version 30 of UsingFreeComment


Ignore:
Timestamp:
02/02/08 01:42:00 (6 years ago)
Author:
Alex Smith <alex@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UsingFreeComment

    v29 v30  
    239239 
    240240== Wrapping post_free_comment to change the redirect url == 
    241 At 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 
     241At 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 original page that the comment was posted from, 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 
    242242To do this you first need to create a wrapper in views.py 
    243243{{{ 
     
    247247 
    248248def my_post_free_comment(request): 
    249     if request.has_key('url') and not request.has_key('preview'): 
    250         response = post_free_comment(request) 
    251         # ensure there's a url to redirect to, and that post_free_comment worked 
    252         if len(request['url'].strip()) > 0 and isinstance(response, HttpResponseRedirect): 
    253             return redirect_to(request, request['url']) 
    254         # fallback on the default post_free_comment response 
    255         return response 
    256     return post_free_comment(request) 
     249        if request.has_key('url') and not request.has_key('preview'): 
     250                response = post_free_comment(request) 
     251                 
     252                # Check there's a url to redirect to, and that post_free_comment worked 
     253                if len(request['url'].strip()) > 0 and isinstance(response, HttpResponseRedirect): 
     254                        return HttpResponseRedirect(request['url']) 
     255                 
     256                # Fall back on the default post_free_comment response 
     257                return response 
     258         
     259        return post_free_comment(request) 
    257260}}} 
    258261 
    259262After you have your view you need to point the requests to that view instead of the default comments view 
    260 To do this add the following line to urls.py 
     263To do this, '''before''' this line 
     264{{{ 
     265#!python 
     266(r'^comments/', include('django.contrib.comments.urls.comments')), 
     267}}} 
     268add this line: 
    261269{{{ 
    262270#!python 
    263271(r'^comments/postfree/', 'views.my_post_free_comment'), 
    264272}}} 
    265 And 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 
     273 
     274And 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 
    266275{{{ 
    267276#!python 
     
    271280)  
    272281}}} 
    273 Once 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: 
    274 {{{ 
     282Once 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. Make your freeform.html look like this: 
     283{{{ 
     284{% load i18n %} 
    275285{% if display_form %} 
    276286        <form action="/comments/postfree/" method="post"> 
    277                 <p>Your name: <input type="text" id="id_person_name" name="person_name" /></p> 
    278                 <p>Comment:<br /><textarea name="comment" id="id_comment" rows="10" cols="60"></textarea></p> 
    279                 <input type="hidden" name="options" value="{{ options }}" /> 
    280                 <input type="hidden" name="target" value="{{ target }}" />  
    281                 <input type="hidden" name="gonzo" value="{{ hash }}" /> 
    282                 <input type="hidden" name="url" value="{{ request.get_full_path }}" /> 
    283                 <p><input type="submit" name="post" value="Post" /></p> 
     287                <p> 
     288                        <label for="id_person_name">{% trans "Your name:" %}</label> <input type="text" id="id_person_name" name="person_name" /> 
     289                </p> 
     290                <p> 
     291                        <label for="id_comment">{% trans "Comment:" %}</label><br /> 
     292                        <textarea name="comment" id="id_comment" rows="10" cols="60"></textarea> 
     293                </p> 
     294                <p> 
     295                        <input type="hidden" name="options" value="{{ options }}" /> 
     296                        <input type="hidden" name="target" value="{{ target }}" /> 
     297                        <input type="hidden" name="gonzo" value="{{ hash }}" /> 
     298                        <input type="hidden" name="url" value="{{ request.get_full_path }}" /> 
     299                        <input type="submit" name="preview" value="{% trans "Preview comment" %}" /> 
     300                </p> 
    284301        </form> 
    285302{% endif %} 
     
    289306{{{ 
    290307        <input type="hidden" name="url" value="{{ request.url }}" /> 
     308}}} 
     309before 
     310{{{ 
     311        <p><input type="submit" name="preview" value="Preview revised comment" /></p> 
    291312}}} 
    292313