How to migrate Django project to Pythonanywhere - python

I am trying to set up a Django app on Pythonanywhere — I've managed to figure out Bitbucket and clone the code in — I deleted the files in the directory that was provided for me — but can't get it to work.
I've done 'syncdb', then when I go to what I think is the correct URL for the app, I keep getting "Unhandled exception" — The error is that it can't find 'portfolio.settings' in an import (portfolio is the name of the app)
I also have no idea what to put for MEDIA_ROOT and STATIC_DIRS — these should be, as far as I know, full paths, not relative.
I'm a Django newbie, and this is proving rather overwhelming, to get the app, which functions fine locally, deployed. Any help much provided (I haven't found the Pythonanywhere forums — which don't seem indexed -- or help all that helpful, I'm afraid)
I also thought: why don't I let Pythonanywhere set up a blank project for me, but again, I don't know how to handle STATIC_DIRS and MEDIA_ROOT, and I don't really know how to make my project fit their setup.
Thanks for any help.

For anyone else that runs into similar issues: import errors in web applications are typically to do with your sys.path not being correctly configured. Check your WSGI file (on PythonAnywhere, you can find it on your web tab. Other hosts may do things differently).
Example:
/home/myusername/myproject/ is the project folder
/home/myusername/myproject/my_cool_app/ is one of your app folders
/home/myusername/myproject/myproject/settings.py is the settings file location
Your WSGI file should have:
sys.path.append('/home/myusername/myproject')
# ...
DJANGO_SETTINGS_MODULE = 'myproject.settings'
And your settings.py should have
INSTALLED_APPS = (
#...
'my_cool_app'
Everything has to line up so that the dot-notation names of your app in INSTALLED_APPS and your DJANGO_SETTINGS_MODULE environment variable will import correctly, relative to the folder you add to sys.path.
So in the example above, you could conceivably do:
# wsgi file
sys.path.append('/home/myusername')
DJANGO_SETTINGS_MODULE = 'myproject.myproject.settings'
# settings.py
INSTALLED_APPS = 'myproject.my_cool_app'
But don't do that, it's overcomplicated.
PS there's a detailed guide to sys.path and import issues for pythonanywhere in the docs.

Related

Unable to fix error DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

Please help resolving this env issue/error. I did found many posts about resolving that error but I still couldn't fix mine. I guess I am just unable to understand it in the right way.
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I have just one app in my Django project, had created using django-admin startproject. The app is added to settings.py under INSTALLED_APPS, and all other settings are set to default by Django itself which i haven't modified.
I have few def in views.py working on models/forms, and those are working well.
I also have created a new folder (myscripts) in the project (next to the app-name folder), and in that myscripts folder i have my other py scripts (other than models/forms). Executing those scripts works, I am able to import those models/forms, it does some work and adds a new entry in django db sqlite3 correctly.
Further, i want to add/update execution status to the db entry (for a given pk/id) after my other scripts finishes its work.
To achieve that, I am trying a new py script which will import models to get its objects so i can update contents in the db but that fails. (throws me above error).
Based on other posts, i did try adding env variable DJANGO_SETTINGS_MODULE=my-app-name.settings, it doesn't work (i just have one app and django has it already configured -- i can see that in wsgi.py as os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-app-name.settings') ).
I also tried code from django.conf import settings , but that also with no success.
However, when I run "python manage.py shell" and import models and its objects they don't throw error but thorws error when i run the update py script.
Please guide me where i can look for to fix the issue.

How to run an django app which is not under the current project?

Actually , I want to run the app which are not in the current project directory. Is it possible to run another app if i give a path to that folder.
Currently i have a project directory PROJECT, under it there is manage.py and PROJECT--->settings.py,urls.py.
And I have an another app running at other directory, for example plugins-->App1, App2.
What i want, when i start the server in PROJECT directory. I want to dynamically configured the settings.py and urls.py of the current project, so that i can run other apps. If some one know how to deal with this type of problem please help me.
Yes, you can. The path to your app in INSTALLED_APPS is any valid python path.
A tuple of strings designating all applications that are enabled in this Django installation. Each string should be a dotted Python path to:
an application configuration class, or a package containing a
application.
Docs: link

Django admin generates static url with /admin/ prefixed

I'm trying to setup a second development environment for my project. I've copied over the settings from the working setup but on the new one the URLs generated by the Django admin are prefixed with /admin/. This does not happen with the other setup at all.
More details:
I'm using django-grappelli for my admin panel.
The generated urls on the working setup are of the kind /static/grappelli while on the not-working one they are of the kind /admin/static/grappelli.
Disabling grappelli yields no fix either, the urls are then of the kind /admin/static/admin.
I'm using Django 1.5, it uses a git repository for the source files and a frozen pip requirements.txt. I can only conclude that version differences are highly unlikely.
Other static files work fine, only the admin related ones are hit with this.
My URL settings are the following:
STATIC_URL = '/static/'
MEDIA_URL = '/static/media/'
The *_ROOT ones are not set due to use of development server.
I'm out of ideas as to why this is happening on only one of the servers with the same setup.
edit:
Removing the /admin/ prefix allows you to properly access the files. The finder is fine, but the urls generated are just wrong.
The fix is very simple, the STATIC_URL setting should always contain the first /. It seems the Django admin prepends /admin/ if there is none.
I had a faulty settings file overwriting my default STATIC_URL, always check your settings first!

Unable to execute Django runserver and no suggested solution seems to work

I've run Django servers on localhost before and have never run into this problem. I'm desperately trying to figure out what I've done wrong.
I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.
As far as I can tell I've configured everything correctly - I'm actually using another functional Django project I built as a go-by.
If I run the following command (or any recommended variation thereof) I receive an error.
django-admin.py runserver localhost:8000
Here is the error:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Can someone please enlighten me as to why this error is occurring, how to fix it and why it doesn't happen with my other Django project?!?
I've found many posts regarding this problem just by doing some quick Google searches, but none of the suggested solutions have helped - nor do I truly understand them.
I'm pretty sure you're supposed to run
manage.py runserver
from inside your project directory. It automatically loads your settings.py, etc.
From the Django docs:
Generally, when working on a single Django project, it’s easier to use manage.py. Use django-admin.py with DJANGO_SETTINGS_MODULE, or the --settings command line option, if you need to switch between multiple Django settings files
Providing some more code or examples of your directory structure might help.
First, the command is generally manage.py runserver 8000, so try that, and that make might a difference.
Second, in Django 1.4, the location of the settings.py file was moved. In previous versions of Django, the directory structure looked like this:
myproject/
settings.py
views.py
urls.py
myapp/
models.py
...
...
However, in Django 1.4, the main project settings and files were moved to a different directory:
myproject/
myproject/
settings.py
views.py
urls.py
myapp/
models.py
urls.py
...
...
So if you're using Django 1.4 but going off of previous examples, your settings.py might be in the wrong place. Additionally, I've found that when running django-admin.py startproject, it sometimes incorrectly creates two settings.py files, once in the old location and one in the new, which could be additionally confusing you. The only one that manage.py would pay attention to is the one in the project's directory.
If it turns out that your settings.py is in the wrong place but you don't want to move it, as your error suggests, you could set an environmental variable called DJANGO_SETTINGS_MODULE as the path to the Django settings.py you'd like to use for your project. I definitely don't recommend doing this.

Django 1.3 - Admin pages do not get the correct ADMIN_MEDIA_PREFIX settings

In my projects settings.py I have set
ADMIN_MEDIA_PREFIX='/static/admin/'
The admin media is being served in the correct location, browsing to http://127.0.0.1:8000/static/admin/css/base.css gives me the base.css for the admin page. But when I inspect the admin pages they are still attempting to find the admin media at '/media/admin/'. I'm not sure what is wrong here. This is what I get when I attempt to find the setting in a manage.py shell.
>>> from django.conf import settings
>>> settings.ADMIN_MEDIA_PREFIX
'/media/admin/'
This should be '/static/admin/'.
James was right. I had a second settings.py defined for my development environment that was overriding my main settings. I've deleted ADMIN_MEDIA_PREFIX from that file and now everything works as expected. Thank you!!
I sometimes get a similar problem where I've clearly made changes but the code still behaves a la pre-change. Sometimes making sure I've saved with fix it, but other times the compiled python files (.pyc) don't seem to update. Deleting the .pyc file sometimes fixes my issue. Note that when you ./manage.py runserver again, the .pyc will be generated with the updated code.
Maybe try deleting your settings.pyc?

Categories

Resources