Django Performance, url.py evaluation and cache - python

I have developed project with django. Now facing few problem with optimizations. I have created/used around 53 small applications. Each applications have their local url.py file with their defined urls.
Now there is a main project url.py. this url.py file define urls and include every application local url.py.
Now Each time when user hit any url, Main url.py file is called, which itself call each of the every single url.py then return view for user to hit.
My Question is very simple that how we can force urls.py to be loaded at once and serve urls directly from cache without evaluating every single url.py on every user hit?
Test configurations:
Django 1.5
python 2.7
Server django test server "runserver"
Deployment Configuration:
Django 1.5
python 2.7
Server apache22 with modwisgi

this is how django processes the HttpRequest https://docs.djangoproject.com/en/dev/topics/http/urls/#how-django-processes-a-request
the first 3 steps are:
Django determines the root URLconf module to use. Ordinarily, this
is the value of the ROOT_URLCONF setting, but if the incoming
HttpRequest object has an attribute called urlconf (set by
middleware request processing), its value will be used in place of
the ROOT_URLCONF setting.
Django loads that Python module and looks for the variable
urlpatterns. This should be a Python list of django.conf.urls.url()
instances.
Django runs through each URL pattern, in order, and stops at the
first one that matches the requested URL.
for your purpose you would need to write your own middleware between 2 and 3 which first looks for a pattern in cache, if it doesnot find, it should go on with step 3.
But I dont think you need this. I would go for caching views which is much heavier than urls.

Related

Is it possible to use the Django Sites Framework with multiple Sites on the same instance?

I have defined multiple sites as the documentation of the Site Framework suggested.
I understand that if I would run mulitple instances of my application with each of them having a different settings file (different SITE_ID), Django would always know which Site to use.
What I was trying to do is to run a single instance, where multiple sites are available, and the right Site should be chosen depending on the current url of the site.
The Sites documentation states:
The SITE_ID setting specifies the database ID of the Site object
associated with that particular settings file. If the setting is
omitted, the get_current_site() function will try to get the current site by comparing the domain with the host name from the
request.get_host() method.
So I tried to remove the SITE_ID from my settings.py and was hoping that Django would check the domain to find the current Site as stated above, howewer this fails with the following exception:
You're using the Django "sites framework" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting or pass a request to Site.objects.get_current() to fix this error.
So it seems like although the documentation suggests otherwise, this setting is not ommitable
I understand that using the Sites Framework like this would lead to problems when there is no Request object available to find the current Site, but this should not be a problem in the context of my application.
Is it possible to use the Sites Framework without hard-coding the SITE_ID in the settings file by just checking the current domain of the application?
I am using Django Version 1.9.9 with Python 3.4.3
The best solution is to simply add the Sites framework middleware:
'django.contrib.sites.middleware.CurrentSiteMiddleware'
This automatically passes a request object to Site.objects.get_current() on every request.
To "check the current domain" you need to have a request - as clearly mentionned in the error message :
or pass a request to Site.objects.get_current()
Else how would the code know the "current domain" ?

Disable sessions in Django

My site serves pages that are almost static and there are no users with logins etc.
I want to completely disable sessions for performance reasons (so django won't access the DB to get the session on every request).
I removed django.contrib.sessions from INSTALLED_APPS, is there anything else I need to do?
You should locate articles how to set up sessions in django and remove everything from settings.py according to such article. Here is it:
Although this should already be setup and working correctly, it’s
nevertheless good practice to learn which Django modules provide which
functionality. In the case of sessions, Django provides middleware
that implements session functionality.
To check that everything is in order, open your Django project’s
settings.py file. Within the file, locate the MIDDLEWARE_CLASSES
tuple. You should find the
django.contrib.sessions.middleware.SessionMiddleware module listed as
a string in the tuple - if you don’t, add it to the tuple now. It is
the SessionMiddleware middleware which enables the creation of unique
sessionid cookies.
The SessionMiddleware is designed to work flexibly with different ways
to store session information. There are many approaches that can be
taken - you could store everything in a file, in a database, or even
in a cache. The most straightforward approach is to use the
django.contrib.sessions application to store session information in a
Django model/database (specifically, the model
django.contrib.sessions.models.Session). To use this approach, you’ll
also need to make sure that django.contrib.sessions is in the
INSTALLED_APPS tuple of your Django project’s settings.py file. If you
add the application now, you’ll need to synchronise your database
using the python manage.py syncdb command to add the new tables to
your database.
So it seems that you should remove middleware too. Maybe it's not necessary, but if you're using static application, then you it's not that bad to remove everything according to the sessions.

Django Oauth calls

I've got a site built in django that I need to make oauth2.0 requests to an external site to get the currently logged in user. Right now I'm just using a test token, however I have to actually register callbacks on my site now. How do I do this?
you must add oauth2 to the INSTALED_APPS list and add some settings constant to the settings.py of the project and append some urls on the main urls.py file of the project and ...
read the django-oauth2 doumentations.

Changing Django's ROOT_URLCONF

We are building a multi tenant website, and some tenants want different URLs and such. No big deal, and this is something we already have fixed by making multiple urls.py which can serve as ROOT_URLCONF. When it comes to a request, we simply use our middleware which sets request.urlconf. So far, so good.
But we are also running Celery for a bunch of nightly tasks, and one of those is sending out emails. Here we don't have a request (obviously), so we can't use that to switch the ROOT_URLCONF that way. But changing django.conf.settings.ROOT_URLCONF doesn't work either (which is kinda logical). Is there any way to switch the URLConf that's being used "mid-process"?
It isn't really an option to run multiple Celery instances, each with their own settings because not all objects can be efficiently linked to the tenant they belong to (the views are different, we start with a User which knows it's tenant).
You can use django.core.urlresolvers.set_urlconf:
from django.core.urlresolvers import set_urlconf
set_urlconf('module.path.urls')
That's what django.core.handlers.base.BaseHandler.get_response uses to set up the resolver for the current request.

How to serve django templates to javascript

I am using django on the server side and obviously javascript on the client side. Now i want to use the plate template engine on the client.
What's the best way so serve django templates to the client? We taught of some ways doing that.
Create a view that serves the raw templates.
probably not the best method
Copy the needed templates to the static folder.
this could be done with a custom static files finder
the broser is able to cache the templates
Provide the templates using a template tag which puts the raw template into a javascript variables.
templates received this way can not be cached seperatly
is a django app out there that makes this easyer?
The reason i need the templates on the client is, that i want to use the same clients on the server and the client side. When the page is first loaded, the full template is rendered on the server, when navigating trough the application only the needed data gets loaded and the page change is done using push state.
If you need to be able to have A) dynamically generated plate templates, or B) dynamically created plate templates (e.g., entered into the DB via the admin, etc.), You'll want to go with 1 (not a bad thing - django is made for serving text content, so as long as you need to have it in a dynamic manner, there's no problem doing it). 3 is a bad choice, because it means that a browser can't cache the static resource (if it's output into each page)... unless you need different plate templates for each page of course.
If you don't need A or B from above, I'd just stick the templates in your static dir, as you mentioned (e.g., collectstatic or simply add them to your repo, if they're a part of your app).
Regarding an app that makes this easy - you could look at Django Chunks (output a static chunk into a place in the page, like `{% chunk "header-snippet" %}), but I don't think you need that.

Categories

Resources