开发者

django connecting twice to database

开发者 https://www.devze.com 2023-02-25 01:37 出处:网络
I made a very simple app in django in which I have one model, when a request comes, I basically filter objects from that model and then render them in the template. It seems to be working fine, but wh

I made a very simple app in django in which I have one model, when a request comes, I basically filter objects from that model and then render them in the template. It seems to be working fine, but when I went to check my datab开发者_C百科ase logs. I found out that django connect twice on every request to retrieve information about the request session. Any one can help me with why this is happening? see below is the example log

110414 18:28:29     8 Connect   root@localhost on project1_dev
            8 Query SET NAMES utf8
            8 Query set autocommit=0
            8 Query SELECT `django_session`.`session_key`, `django_session`.`session_data`, `django_session`.`expire_date` FROM `django_session` WHERE (`django_session`.`session_key` = '7af6952f847471091a83ee9382bb858d'  AND `django_session`.`expire_date` > '2011-04-14 10:28:29' )
            8 Query SELECT `category_main`.`id`, `category_main`.`slug`, `category_main`.`is_active`, `category_main`.`site_id`, `django_site`.`id`, `django_site`.`domain`, `django_site`.`name` FROM `category_main` INNER JOIN `category_main_i18n` ON (`category_main`.`id` = `category_main_i18n`.`main_id`) INNER JOIN `django_site` ON (`category_main`.`site_id` = `django_site`.`id`) WHERE (`category_main_i18n`.`language` = 'en'  AND `category_main`.`is_active` = 1 )
            8 Quit  
            9 Connect   root@localhost on project1_dev
            9 Query SET NAMES utf8
            9 Query set autocommit=0
            9 Query SELECT `django_session`.`session_key`, `django_session`.`session_data`, `django_session`.`expire_date` FROM `django_session` WHERE (`django_session`.`session_key` = '7af6952f847471091a83ee9382bb858d'  AND `django_session`.`expire_date` > '2011-04-14 10:28:29' )
            9 Quit


Are you, by any chance, serving your media files using django ? that would create additional requests and thus additional query.

The "session" query is done on every request, that's why I think you may be doing more requests than you think.


I experienced the exact same thing. In my case it turned out it was a request for favicon that was the cause. It did not show up in the Chrome network console, nor in the access log from manage.py. I solved it by simply adding a facicon.ico for my site.

I also tried the debug toolbar, but the SQL queries did not turn up there, probably because they where not associated with a view.

0

精彩评论

暂无评论...
验证码 换一张
取 消