Reputation: 11052
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:
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?
Upvotes: 1
Views: 1094
Reputation: 25164
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
.
Upvotes: 7