Re: request.session in template tag?
Thanks to a comment on my blog: the below is null and void: the request object can be retrieved directly from context: context ['request']. On Jun 2, 2:55 pm, BluMarble wrote: > On Jun 1, 7:53 pm, "bax...@gretschpages.com" > wrote: > > > I'm trying to write a template tag that accesses the user's > > request.session. My problem is, I don't know how to get the request. > > > Googling around, I saw references to including > > TEMPLATE_CONTEXT_PROCESSORS in settings, but I'm still not seeing the > > request. > > > Help? > > Hi your best bet is to do the following - I used this for something > else but you'll get the idea: > > from django.template import resolve_variable > from django import template > from django.template import Library, Node > > register = template.Library() > > class AlertNode(Node): > def __init__(self, request): > self.request = request > > def render(self, context): > request = resolve_variable(self.request, context) > > # Do something with the session > var = request.session.get('js_alert', None) > if var: > del request.session['js_alert'] > return str('
Re: request.session in template tag?
If you just need the request obj passed to all pages. I use this in my URLs: # Custom Direct to template - carrys the request object def direct_to_template(request, template): from django.shortcuts import render_to_response from django.template import RequestContext return render_to_response(template, {'request': request}, context_instance = RequestContext(request)) urlpatterns = patterns('', (r'^$', 'urls.direct_to_template', {'template': 'index.html'}), ) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: request.session in template tag?
On Jun 1, 7:53 pm, "bax...@gretschpages.com" wrote: > I'm trying to write a template tag that accesses the user's > request.session. My problem is, I don't know how to get the request. > > Googling around, I saw references to including > TEMPLATE_CONTEXT_PROCESSORS in settings, but I'm still not seeing the > request. > > Help? Hi your best bet is to do the following - I used this for something else but you'll get the idea: from django.template import resolve_variable from django import template from django.template import Library, Node register = template.Library() class AlertNode(Node): def __init__(self, request): self.request = request def render(self, context): request = resolve_variable(self.request, context) # Do something with the session var = request.session.get('js_alert', None) if var: del request.session['js_alert'] return str('alert("%s");' % var) else: return '' @register.tag(name="get_js_alert") def get_js_alert(parser, token): try: tag_name, request = token.split_contents() except ValueError: raise template.TemplateSyntaxError, "%r tag requires exactly one argument" % token.contents[0] return AlertNode(request) # Usage: {% load js_tag %}{% get_js_alert request %} You now have acces to POST, GET and SESSION ect in your templatetag. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: trouble with Django and javascript
I personally feel you should look into doing the post via Ajax, pass your form field values into an array and unpack the values in your view: ie: function AjaxSubmitForm(formdata){ $.post('/ajax/some-view/'+formdata+'/', function(data){ if(data == 'done'){ d.body.innerHTML = "This is a test"; } }); } JSON would be a better option but this will work well. See more at: http://docs.jquery.com/Ajax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: @cache_control() and @never_cache not working
Thanks for the help Thomas, unfortunetly it wouldn't be within budget to port this site over to 1.0. Will however look into the link you provided. On Jan 29, 12:50 pm, Tomas Kopecek wrote: > In that version (0.96) of Django was bug in combination of these > decorators and CacheMiddleware. If you can use upstream version then > everything will be working. Otherwise look for these (closed) tickets > onhttp://code.djangoproject.comand patch your version. > > Tomas Kopecek --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: @cache_control() and @never_cache not working
Anyone? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
@cache_control() and @never_cache not working
Hi, I'm pretty new to django so please bear with me. The Server: lighttpd python 2.5 django 0.96 The Code: >>> settings.py # Cache settings CACHE_MIDDLEWARE_SECONDS = 5*60 CACHE_BACKEND = 'file:///var/www/data/some_project/tmp/file_cache/? timeout=300&max_entries=500' # Middleware MIDDLEWARE_CLASSES = ( 'django.middleware.cache.CacheMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.doc.XViewMiddleware', ) >>> views.py @never_cache def some_view(request): ... @cache_control(no_cache=True, max_age=0) def some_view(request): ... Okay so I think i've got the above right but all the views with the above decorator cache anyway and as this is being used for clients to upload and manage property listings. I'm stumped can anyone help? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---