Search code examples
djangodjango-viewsdjango-templatesdjango-ajax-selects

How to pass a "request" parameter at the time of calling a method in template tags in django


I am stuck in a very weird situation. There is a two method defined in templatetag/somefile.py with named : ajax_print_widget and render_widget

 def ajax_print_widget(request, template, context1 ):
 if request.is_ajax():
     q = request.GET.get( 'q' )
     if q is not None:
         data = {
             'results': context1,
         }
         import pdb; pdb.set_trace()
         return render_to_response( template, data, 
                                    context_instance = RequestContext( request ) )

 @register.simple_tag
 def render_widget(widget_settings):
     widget = widget_settings.get_widget()
     template_name = widget.template_name
     context = widget.context(widget=widget_settings)
     t =  get_template("widgets/%s" % template_name)
     return ajax_print_widget(t,context)

My main motive is to pass the data using Ajax (as i am checking in ajax_print_widget method) and render it to a template and rest of the things will be handled by jquery in a given template name. To perform this things i am performing the followings steps:

    1. In render_widget method i am getting two variable which i need for Ajax purpose i.e. template name ('t') and the required data ('context')
    2. Now i need to pass these two variable as a parameter to ajax_print_widget method by calling it at the time of returning from render_widget (see the code)
    3. As you see that i need an extra parameter i.e. 'request' in ajax_...method (required for checking `request.is.ajax():`). But i am not able to find out any way to send this parameter to ajax..method

I tried this by writing :

return ajax_print_widget(request, t, context)

but it is showing an error i.e. request is not a global variable (that is obvious) and if i leave rest of the code as i pasted it above then it shows another error i.e. ajax_print takes 3 arguments and 2 are given (right) Now i am not getting any hint to solve this issue and without passing request it can't work for me.

Apart from that i have an doubt about template tag. I even search this render_widget method and i didn't found at any file where from it got called. Would you also please tell me the significance of template tags. The methods are defined in template tag are called from somewhere or why we write this in template tag?


Solution

  • If you enable django.core.context_processors.request in your TEMPLATE_CONTEXT_PROCESSORS setting (it's not in there by default) and change your tag to takes_context=True then you can get the request inside your template tag. https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#simple-tags

    @register.simple_tag(takes_context=True)
    def render_widget(context, widget_settings):
        request = context.get('request')
        # Rest of the tag goes here
    

    Note that this also requires your template to be rendered with a RequestContext.