3

I store my sites navigation menu in database. I want to load all objects in the list once I launch my server, but I haven’t got idea how to do it. I really need to do it, because it will be problem in future to load data from database in views to display menu.

I tried to put loading code in settings.py, but there was an error, and in views after imports, but there was no effect.

Lertmind
  • 53
  • 5

3 Answers3

1

Well, I'm very new to django, and maybe I'm wrong, but I think you are looking for something like caching. Read the docs, and decide whether is it fits you or not.

balazs
  • 5,698
  • 7
  • 37
  • 45
  • I have got same problem, so I think it is wrong way to store data. – Dracontis Aug 11 '11 at 20:47
  • @Art In an [another](http://stackoverflow.com/questions/2680902/python-django-global-variables) Question others recommend cache. But everything depends on your needs, or in this case in your data. And in this situation we don't know anything about what objects Lertmind want to load on startup. – balazs Aug 11 '11 at 21:10
  • I have got the same problem and I really need to solve it. I have got question - how works generic views? I have read article in djangobook, but I don't understand one thing - is data loading in dict once or every time user request the page? I think this can be solution, but I need to know mechanic of it. – Dracontis Aug 11 '11 at 21:25
  • @Art generic views and Lertmind problem are completly different things in my opinion. Anyway.. there's a database query for every user request. – balazs Aug 12 '11 at 00:04
  • @art class-based generic views are initialized once so you can avoid a lot of queries. Read the django 1.3 documentation. – rewritten Aug 12 '11 at 04:53
  • @balazs view caching avoids just that, you can even render a piece of page and cache just that piece (i.e. the navigation menu), so you avoid to do queries for cached content. – rewritten Aug 12 '11 at 04:54
  • @saverio I recommended caching too :). See my answer, which is the same you gave. For me the new thing is that generic views initializes once. – balazs Aug 12 '11 at 09:45
  • 1
    Class-based generic views, being classes, execute something in the request/response phase, other things just on instance initialization. By carefully choosing where to put your code, you can avoid multiple execution of various things. On the other side, fucntio-based generic views are always executed entirely for each request. – rewritten Aug 12 '11 at 11:05
1

I think you need Template context processors.

Here is a nice tutorial by James Bennett on that:

http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

mohi666
  • 6,842
  • 9
  • 45
  • 51
1

You can cache just a part of your view:

https://docs.djangoproject.com/en/1.3/topics/cache/#template-fragment-caching

surround your navigation bar like this:

{% cache 500 navbar %}
   ... put your navbar code
{% endcache %}

and ensure to have

{% load cache %}

at the top of your template or the base template.

rewritten
  • 16,280
  • 2
  • 47
  • 50