Constantly need to reset django local settings module - django

I have made two settings files, one for local testing and one for production (heroku). Every time I pull and start working locally I need to run the following two commands for it to work:
export DJANGO_SETTINGS_MODULE=
DJANGO_SETTINGS_MODULE=projectName.settings_local
Without doing so I get the error that the local settings module cannot be found. This error appeared after I tried to have both settings files in a settings folder, which I did not get to work so I put them back in the original place. Does anyone have an idea how I can fix this?

In your wsgi.py file setdefault settings file path, in your case local setting file
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectName.settings_local")
application = get_wsgi_application()
You may also need to add in manage.py file
.....
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectName.settings_local")
.......
I hope this will help you.

Related

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).

Module 'main' has no attribute application -- Google App Engine Python with Django

I have been running into this problem for a short while now and simply can't find a solution anywhere. I am using Google App Engine to run a default Python 2.7 app with Django 1.5 (via GAE SDK) created through PyCharm. I can upload the app successfully, but upon visiting the actual page, I get a Server Error. Then, checking the logs in Google App Engine, I see this:
ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application
After searching the internet for a while, I was able to find a few posts which address this issue, but attempting them never seemed to solve my problem. For example: This problem was solved by replacing "application" with "app" in the following lines:
application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)
In fact, I had run into this same issue before and this solution provided a fix for me in the past, however at that time I was running a separate app and it was not through the GAE.
I checked the Django documentation for version 1.5 here, but the code and suggestions there don't seem to conflict with what I currently have in my project.
I read a bit more about this type of problem, and saw another post that suggested checking the app's wsgi.py file to ensure that it is named 'application' or 'app' respectively, so that one could then use that same name throughout the rest of the application. However, upon checking those settings I saw that 'application' was used there too:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
There's even a line in settings.py which uses the same nomenclature to declare the WSGI application:
WSGI_APPLICATION = 'Chimera.wsgi.application'
I'm really having trouble debugging this. I get the feeling it's really dumb and I just can't see it, but unfortunately I'm not particularly good at this kind of stuff -- I'm still a bit of a novice in this field.
Does anyone have any idea what I could try in an attempt to fix this issue?
UPDATE: I started making line by line changes and testing things, and eventually I found that the GAE log changes depending on the input for the "script" under app.yaml. So if I change the script under "handlers" between "main.app" and "main.application", it adjusts the log output to refer to "app" or "application" respectively. So that line in the app.yaml file tells the app what to look for, but I'm still not seeing why it can't be found. Not sure what else I could change to test it out. I wish I knew a bit more about the actual inner workings so that I could figure out why the app is confused about the attribute. Is it trying to run before it even gets instantiated or something?
Source code below:
main.py
import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'Chimera.settings'
from google.appengine.ext.webapp import util
from django.conf import settings
settings._target = None
import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher
def main():
application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
app.yaml
application: eloquent-ratio-109701
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.application
libraries:
- name: django
version: 1.5
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Chimera.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Full log from GAE:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 302, in _LoadHandler
raise err
ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application
Thanks for helping me out.
In your main.py file (i.e. the main module) application is a variable inside the main() function, not an attribute of the main module. Basically you don't need a main() function.
GAE has some specific support for using Django, I'd strongly suggest going through the Django Support documentation and the Django App example.
Based on the comment made by #DanielRoseman I discovered that declaring the app inside of the main() function caused an issue because the app attribute was then only accessible at the main() function level, as it was a member variable of main() as opposed to a global variable. Although the default application files were structured this way by PyCharm, it seems that it was incorrect. I'm not sure if this is a compatibility issue, but regardless, moving the app declaration outside of the main() function adjusts the scope in a way which allows for other parts of the project to access it, solving my problem.
Thank you #DanielRoseman for the comment.

PythonAnywhere wsgi deployment error

I've followed the steps and everything works fine on a local server but when I try to deploy on PythonAnywhere I keep running into problems. I don't know what I'm doing with the WSGI file so I just copied and pasted a template with some adjustments:
import os
import sys
path = '/home/KTruong88/Kappa_Ranks/Kappa_Ranks/'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'Kappa_Ranks.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application
I try to run the thing, and it gives me in the error logs:
TypeError: get_wsgi_application() takes 0 positional arguments but 2 were given
I don't know how if I configured my wsgi file properly, and I don't know where I can even access the get_wsgi_application() function so I can adjust it, or if I could, what would I adjust it too. How can I fix this?
You did not need to "copy and paste" a WSGI file in the first place; it is included in the project that was created when you did django-admin.py startproject.
Nevertheless, the problem is that application should be the object returned from get_wsgi_application, not the function itself:
application = get_wsgi_application()

Django deployment, cannot import settings

I'm trying to deploy an application with django.
I've put my django_project directory in /home/XXX/websites/YYY.
The web server root is in /srv/http/YYY and it only contains a directory named static and an apache.wsgi file.
The content of apache.wsgi is as follow:
import os, sys
sys.path.append('/home/XXX/websites/YYY')
os.environ['DJANGO_SETTINGS_MODULE'] = 'YYY.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Whenever I reload apache modules and try to load a page, I got an import error :
ImportError: No module named YYY.settings
I don't understand. Since the settings.py file is in /home/XXX/websites/YYY/YYY/settings.py, why this don't work ?
I've manage to make this work by putting the django project in the web server root directory, but that wasn't the place where I wanted to put it.
Thanks for your help.

django automate syncdb

I'm working on a project that includes a django server, and also a setup module.
The user will be configuring their system to run my program, which includes a django webserver element along with other items. I'm working on a setup module that assists the user in getting all of the settings correct and sets up all of the appropriate files. One of the things that I'd like to be during the setup process is essentially a "manage.py syncdb" command that creates an appropriate SQLite file and table from nothing.
I could grab the code found in manage.py and directly stick it into my setup module appropriately, but I'm not sure if there's a better approach that I'm missing - along the lines of two lines consisting of:
import django.something
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.something.syncdb()
Or something of the sort. Am I just missing something here?
This should do it:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core import management
management.call_command('syncdb', interactive=False)
You can also do
import os
import settings
from django.core.management.commands import syncdb
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
syncdb.Command().execute(noinput=True)