I am deploying social networking site with django on VPS, my current stack is nginx, postgresql, gunicorn. I am going to add memcached, I will be forced to use {% cache %} tag in my templates or low level api in views though, as each site will be dynamic for authenticated users. I have 1 question regarding {% cache %} tag, lets say i will input a variable within cache tag like so:
{% cache 500 x %}
{{ variable }}
{% endcache %}
Lets assume that {{ variable }} is in fact very consuming database query written in related view. What would be the best way to prevent database hit from view (lets assume query will be evaluated in view) , should i cache it as well with low level api? If so isnt it a bit redundant as I would cache it in both places? Please give me your thoughs what would be the most convenient method. Btw, I know that this example is trivial, if I only wanted to cache variable I could do this in view, but I will surely need to use cache tag to cache some loops, multiple html lines generated by python etc. Thanks
First, the usage of authentication in your site doesn't involve the usage of low level cache in both sides views and templates, note for instance you can do something like {% cache 500 user %} and that will save different chunk of code of each user of your site.
Answering your second question: in general, you can prevent database hits just caching into the templates. Evaluate the query there instead of doing into the view, and make a dynamic clean up of the keys when the content changes instead of using fixed expiration times, that way let you get better performance result. Also, take in mind cache tag saves just chunk of HTML code.
Related
I have a template that is a header snippet that is used on every page of the platform.
What I would like to know is what performance implications/issues checking permission with {% if perms.foo.add_bar %} can have on the platform. And if there is a better way to do this.
I checked the load/request time of the page on Django Toolbar and didn't seem to change at all. Although I tested just at my local environment, not the production (don't have access).
I have the website where I need to limit users access to the website's parts. To keep it simple let's say while website is in beta - I want only registered users can see the website and all the parts. But later I will want to remove those limits.
What I can do - do this login in the template level. Like in all the templates I can have {% if user.is_authenticated %} and then just show some message if not.
Second thing which is in my minds - I can define middleware which will check if the user is logged in and if not - redirect him to the login page.
I see bad thing about the template solution, because after I will release the website out of beta, I will need to modify a lot of templates...
What else I can choose here?
Thanks!
You must tag all methods or classes involved with the login_required decorator. If you're using generic views, set login_required=True when defining them in urls.py.
I'm using AppEngine and django forms passing template_values and using base.html with {% extends "base.html" %} this works well in a testing environment when I have one big form for adding entities.
I'm good in python, good with the datastore, but my html is weak/OLD
I want to have a step by step process for creating datastore entities. In broad strokes, what are some common methods/search-terms I should be using? I've heard that I might be hitting a bit of a limit at some point by using django instead of some other framework - am I already at that point?...can't be?...
Sounds like you are looking for Django Form Wizard?
http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/
I have a blog written in Django, and I started using the basic Django caching middleware with the file system caching backend. Unfortunately, this led to two things being cached that shouldn't have been: admin links (e.g. "Edit this post") for logged-in users and
prepopulated comment forms based on cookies.
To get around that, I started using the template cache tags:
{% load cache %}
...admin links...
{% cache 500 blog_entry entry.id %}
...entry...
{% endcache %}
...comment form...
But it seemed that the whole page was still getting cached as well. How do you set up the caching system to only cache the parts of the template you explicitly set?
Edit: For the comments, if someone comments on the blog, I store their name, website, and email address in the session variables. If they come back to the site, then I prepopulate those parts of the form with that data. But that means it is possible for the caching system to cache a view with prepopulated data, which is not good.
You need to remove the caching middleware now that you are caching template fragments instead of entire pages.
Add this to your settings.py:
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
This should fix the issue with admin links, I don't quite understand what your second issue (prepopulated comments) is?
after caching several views on my django project -#cache_page(60 * 5)- I've noticed that memcached caches the entire view, even the request argument! So if the first user that visits a page is logged in as userxyz, all other anonymous or registered users that will ask the same page will be presented with the page that was cached by user userxyz! Obviously this is not a desired behavior...So can I cache everything on the view, but not the request argument? Or memcached is suitable for anonymous sessions only?
Thanks in advance,
Markos Gogoulos
If you're mixing dynamic and static data on one page, in your case the dynamic data is the logged in user's username, then page caching isn't the right choice. This wouldn't change if you were using file based cache storage instead of memcached.
I suggest trying fragment caching. You can do something like this:
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
This will cache the contents of the cache tag for 500 seconds with the identifier sidebar.
You can find more information on caching here:
http://docs.djangoproject.com/en/dev/topics/cache/
If this is a page that is going to be hit very often, for example a welcome page, that you feel would benefit from using page caching over fragment caching (for example the only dynamic data is the user name), then there are a few other options.
Say for example you want to have a completely static page except for a login/logout section at the top which displays different links depending on whether or not the user is logged in then you can check for the existence of an authentication cookie when the page is first loaded and conditionally display different data using javascript.
Memcached is just a backend. It caches whatever you tell it to cache. So really your question is "Is Django's full-page caching suitable for dynamic pages?"
Probably you don't want do cache full-pages, just part's of it. Or only pages for anonymous requests (using CACHE_MIDDLEWARE_ANONYMOUS_ONLY)
Refer to the book
http://www.djangobook.com/en/1.0/chapter13/
You might want to look into template fragments and caching those bits of content that aren't user specific.