Django: Manage.py runserver no error, but mod_wsgi report cannot import name connection - django

When testing my django website by python manage.py runserver 0.0.0.0:8000, there is no error.
But if I deploy it to production with apache and mod_wsgi, it reports error cannot import name connection.
I find some other questions like django 1.4 database router - "cannot import name connection" suggesting adding from django.db import connections into settings.py.
But I found adding this import can prevent this error, but it also disables database router.
I think it may due to different running environment between manage.py runserver and WSGI.
This is how my wsgi.py looks like
import os
import sys
sys.path.insert(0,"/home/my/myweb")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How can I fix this bug?
My python version is 2.7 and django version is 1.4.3
Edit 1:
My OS is CentOS 6.4, on this server:
If I run it using python manage.py runserver 0.0.0.0:8000, no error.
If I remove database router, it can work under WSGI.
But database router and WSGI cannot work together.
Any advice is appreciated.
Edit 2:
Thanks to #Graham Dumpleton, I figured it out by myself.
This is the wsgy.py that works for me.
import sys
sys.path.insert(0, '/home/my/myweb')
from mywebsite import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Usually caused by using a mix of imports where some imports are via the site package and some don't. You can add:
sys.path.insert(0,"/home/my/myweb/mywebsite")
and that may help.
For a bit of a discussion of the problem see:
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

Related

Deployment of a Django project on Gandi server

While trying to deploy my django project on my Gandy hosting server, while starting running uwsgi, the uwsgi.log shows an error
ModuleNotFoundError: No module named 'django'
while executing the following line from my wsgi.py file
from django.core.wsgi import get_wsgi_application
here is the full wsgi file:
import sys
import os
import os.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'projectname')))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from what I understand, django is not installed in my virtual environment, but it is listed in my requirements.txt file
Django==3.0.6
django-background-tasks==1.2.5
django-compat==1.0.15
django-extensions==3.0.3
django-mathfilters==1.0.0
Can anyone pin point where I am missing something?
I realise you've probably moved on from this now!
In case others hit the same issue, you need to deploy your code, not just upload it, for the Gandi Python environment to read requirements.txt and build your code.
See https://docs.gandi.net/en/web_hosting/connection/git.html?highlight=deploy#deploy-your-code

Error while deploying Django app on cpanel(shared hosting)

I m new to django. I created a web with dajngo,and successfully deployed it in the server
The python app has been successfully setup and virtual environment has been setup.
but while running the web it gives me "Server Error (500)" I don't know whats the problem.
I think error is in "wsgi.py" file but i'm unable to idenify it.
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'karan_web.settings')
application = get_wsgi_application()
my "passenger_wsgi.py" file is:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'karan_web/wsgi.py')
application = wsgi.application
can someone help me with it;
Sorry for the late answer, I had figured out the as to this just forgot to post it.
As I stated in my question, the actual problem was in passenger_wsgi.py, the django server starts with the wsgi.py and act as the gate way to the Django server.
So whenever a Django project is uploaded on the hosting server, It creates a passenger_wsgi.py file and one default wsgi.py, address of which is provided in passenger_wsgi.py by default.
So we just need to change that address and provide the address of our own wsgi.py in the project
In my case it was
import os
import sys
from karan_web import wsgi
application = wsgi.application
just edit in passenger_wsgi.py the following code.
from karan_web.wsgi import application
You need to check if your code syntax is correct and running properly. If its still doesn't work try to delete and recreate your database in cpanel and check if you have made all necessary migrations, don't forget to restart your python app. If after all these it still doesn't work check if all your files have the correct file permission(766).

How to get raven to report Django runscript exceptions to Sentry?

My Django web app logs exceptions to Sentry via raven. I also run a number of scripts (via manage.py runscript) as cron jobs. Right now any exceptions in those scripts are not being reported to Sentry. How do I set such reporting up?
As of version 5.3.1 of raven-python it should correctly patch Django's BaseCommand.execute, which effectively will handle errors in these commands (unless that parent call is never made).
For those out there that:
still have an issue of raven not patching itself for django management commands correctly
have Django==1.6.11
haven raven==5.12.0
I have found a fix that works for me.
The problem seems to be that raven is not patching BaseCommand.execute by the time it is called by Django. So to fix that, I make sure BaseCommand.execute is patched right away. I've updated my manage.py file to include the following lines:
from raven.contrib.django.management import patch_cli_runner
patch_cli_runner()
My final manage.py file looks like this:
#!/usr/bin/env python
from os.path import abspath
from os.path import dirname
import os
import sys
if __name__ == "__main__":
# add the ../ directory to the os path, so that we can find
# app.settings below
sys.path.insert(0, os.path.abspath(os.path.join(dirname(abspath(__file__)), '..')))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
from django.core.management import execute_from_command_line
from raven.contrib.django.management import patch_cli_runner
patch_cli_runner()
execute_from_command_line(sys.argv)

what's the choice should be done with django_wsgi and wsgi

My current version of django is 1.6.3
And i want to deploy django on my centos server with uwsgi and nginx.
There's a tutorial posted by someone who use 1.3's django that says it should create a file named django_wsgi.py inside my django project. I was wondering if this step could be ignored because i already have wsgi.py in my project at beginning. Are django_wsgi.py and wsgi.py the same thing? Just change the name along with the version upgrade.
#django.py
mport os
import sys
sys.path.append("/opt/www")
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
#wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netmag.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The project structure generated by startproject changed since 1.3. Before 1.4 release, you had to manually create a wsgi file. Since 1.4, Django automatically creates wsgi.py file in the project root.
Continue using wsgi.py that Django created for you.
A Django 1.6 already has a default WSGI configuration as you can see in your wsgi.py file, nothing else is needed. The instructions you mention are for legacy Django projects.
See https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ for more information on the Django WSGI config.

Applying Django 1.6 project settings in PyDev

I have no problems running the python shell with the python manage.py -shell command in the terminal; I can import my modules and make queries on the database and so on. However, in PyDev, even though I can import modules, when I try to access the data stored in my SQLite database, I get this message:
ImproperlyConfigured: settings.DATABASES is improperly configured.
Please supply the ENGINE value.
Since my project's settings are ok (the site works fine locally), it must have to do with Pydev not applying the project configs. The sequence of starting up the Django/python shell is as follows:
from django.conf import settings; settings.configure()
from django.core import management
import XX.settings as settings
management.setup_environ(settings) # This throws an error as setup_environ
# setup_environ is deprecated in Django 1.6
The last 3 lines are hard-coded (and were, I gather, working pre-Django 1.6)
I thought doing something like:
from django.conf import settings as djangoSettings
from XX import settings
djangoSettings.configure(settings)
But then I get this error:
ImportError: Could not import settings ''XX.settings'' (Is it on
sys.path? Is there an import error in the settings file?): No module
named 'XX.settings'
And yes, the path is in sys.path.
Any help greatly appreciated.