Django application onload? - django

Is there a way to hook on the loading of the django application?
I want to be able to execute code when the application is loaded, so that I can for example
create static variables to be used later on by the application or establish connections to other servers.
The best I came across was to add code in the __init__.py file (How do I create application scope variable django?) but the problem with this solution is that I want my code to be executed after django has finished its startup process, and not in the middle/start of it.
Another solution I came up with is to have a view that handles this process and then when the application is deployed I issue a request to the url of the view. I don't like this solution very much, I prefer it to be a part of the loading of the application.
Any ideas of how to pull this out?
Thanks;
edit: Apllication refers to the entire django project and not one of the INSTALLED_APPS

Right now, there's really no good way to do this as Django doesn't have a startup signal. Interestingly, there is a ticket for this, but it's strangely tied to a branch that is being held up by another ticket. I'm not sure if Django 1.4 is feature-locked, yet, but as it's in release candidate stage, my bet is that it is. So, maybe you might get this in Django 1.5 whenever that happens.

Related

how to update FrontEnd in real time when the database get update from other apps?

first of all many thx in advance.
I have deployed my django(1.11) project to azure, it works together with the postgresql db. Now for the frontEnd I would like to build up a live chart/figure, which means that every time when the postgresql db receive any update (from the function app on azure, not the django self), my chart/figure should be automatically refreshed.
I have tried Signal, but it only react to the update which is made by django self. Then I try channels + redis, followed up the example(chat), but still not sure how to make it.
my question is: which package/ components/ Tech should I use in this case? can you guys give me some example or a link to the certain tutorial?
Look forward to the fancy idea from u guys.

Auto populate django models and views while creating an app

I am pretty new to Django and still learning. The question is that i know that an empty app can be created in django and then i can add models and views. But throughout a long time of making websites, I noticed that most of the time i am doing operating on dbs like update,del or insert. Now my question is that is there anyway that i can add the model parameters while creating the app itself in django and it automatically populated models,forms and views for me for the basic operations that i mentioned above.
That way i can create DBS architecture while i am creating the app and the only way that i am left with is to review and make changes in models if any.
Just to make sure that i am understood, i am attaching a snippet of what i am trying to say which is wrong but i hope that it gives a better idea
//Not real code Purpose only to give a better understanding to question
python manage.py startapp test --fields id=int verified=bool
Or if there is not is there any zsh plugin / 3rd party plugin that might be helpfull
Also i should mention that the need originated from a current project which is a one page application and has lots of tables but no operations on it. So the task to write models everytime would be tedious and not the correct way. So any other suggestions are also greatly appreciated

Uploading Python application for the first time. URLconfig URLS not working

It is my first time trying to get a Django application live on the net but I am having an issue with the URls.
The working local URL of my application is
http://localhost:8000/surveythree/ - This works as expected.
However when I upload my project to my hosting account I cant seem to locate the relevant page using the shortened URL as provided by the URLconf, in this case it should be /surveythree/
url(r'^surveythree/$', SurveyWizard.as_view([SurveyForm1, SurveyForm2, SurveyForm3, SurveyForm4, SurveyForm5])),
I can locate the page if I use the full filepath however.
http://www.mywebsite.com/bias_experiment/src/survey/templates/formtools/wizard/wizard_form.html
I thought the benefit of the URLconf was to shorten the URL to something like one of the following
http://www.mywebsite.com/bias_experiment/surveythree/
http://www.mywebsite.com/bias_experiment/src/surveythree/
http://www.mywebsite.com/bias_experiment/src/survey/surveythree/
Is there something simple that I am missing here? If anyone could tell me what the shortened URL should be based on the above it would be great. I have been trying multiple combinations for a while now but I don't know if I am going around in circles or doing it wrong.
Thanks in advance.
It doesn't look like you have actually deployed your site with a proper server. You can't just upload the files to any webserver and expect then to run: you need to configure a wsgi server and connect it to your app.
The documentation is here but to be honest I'd be amazed if your college server supported it at all, if all you have is a shared folder. You may be able to get it to work with FastCGI, but I wouldn't hold out a whole lot of hope.
(And even though you say that going to that long URL "works", I'd guarantee that all you're seeing is the raw HTML template. There's no way that any actual dynamic functionality will be working like that, as you'd see if you actually tried to submit the form at that URL.)
Deploying Django apps is much more complicated. To run in more "production" environment you will need to configure:
virtualenv to keep pip modules which your app required separate from global environment.
nginx for hosting static files ( you can copy them to some folder with ./manage.py collectstatic.
WSGI server: uWSGI or Gunicorn are both nice choices.
supervisor: for running and restarting WSGI and any other apps (for example celery) running in background
It's a lot for a start, so it's good to follow some tutorial and use ready to use config snippets.

Tricky issue with django sessions: sometimes session information is erased

I have a weird bug with django sessions in my app: some times (about 10 times for ~20000 per day) session information for user is erased. I traced it via log files: at page A there is information for user's session, after it he submits the form and at the next page his session is empty. I tried two types of storage: memcached+db and db only and this problem is for both of them. I tried to reproduce these scenarios, but all works as expected, as I said, it happens very rare. I also checked that this problem exists for different users, and for them is doesn't reproduce each time. I don't have any ideas how to catch the root cause and I don't know what else post here as a description. If someone has any ideas, please let me know. If it is important, I'm running my app with django 1.2 + FastCGI.
Thanks!
UPD: I checked and see that session key from uses is not changed during two sequential requests, at first request there is an actual session state, and at second session variables are relaced with empty.
As a way to debug this problem, I would subclass the standard Django session middleware (or whatever you're currently using):
django.contrib.sessions.middleware.SessionMiddleware
and wrap process_request and (probably more importantly) process_response in some extra logging. Then install your subclassed session middleware in the MIDDLEWARE_CLASSES, rather than the stock Django one.
You could also validate that session.save() has actually committed its changes by attempting to read it back. It could be that the problem lies in session-state serialisation, and it's failing on a particular key or value that you're attempting to store.
None of this will fix your problem, but it might help you to establish what's going on.
As #Steve Mayne mentioned, it would be good to do some logging on the sessions middleware and sessions model save method. That's something I'd start with.
In addition I'd like to say that this could be a database related issue, especially if you're using MySQL database backend for sessions. You can check the log for database locks and other concurrency issues. I had to deal with similar issues before and the solution is clear: optimization and additional performance.
If you have some specific application middleware, you can check for functionality that interferes with Django sessions. Such parallel operations can cause problems, if not implemented properly.
Another thing I would do is to upgrade to the latest stable release of Django and migrate to a mod_wsgi setup.

Loadable/Unloadable django apps

I wanted to make an application which could handle other applications as plugins, that the user could download and load/unload at any time. I read all the django documentation, and there doesn't seem to be a proper way to do it apart from installing the app by hand a doing a syncdb, with no possibility of unloading.
A good example of what I want to do could be the wordpress plugins. I wanted something like that for my django project, downloadable "plugins" that the user can load and unload at any time.
Is there any way to accomplish this?
I don't think it's possible. Django loads all its .py files only once, when its *cgi process is started. So to updating your urls.py or models.py requires restarting cgi, and I don't think you want to allow your users doing this. :-)