Can't sign in to Django admin - django

I can't sign in to Django admin with any of the superusers I've created. Tried creating new superusers, changing passwords, etc. - no error messages for any of these processes, but still can't sign in.
I'm not sure if it's related, but I also can't run django-admin.py commands. Here is what I've done so far:
$ django-admin.py validate
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
$ export DJANGO_SETTINGS_MODULE=mysite.settings
$ django-admin.py validate
Error: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings
sys.path shows ['/Users/joerobinson/Sites/django/mysite' ... (other stuff on path)]
Is there something else I need to do to register the mysite module?
python manage.py runserver works fine.
Conclusion
For the question about logging in to django admin, I did not have django.contrib.auth.backends.ModelBackend included in my AUTHENTICATION_BACKENDS - adding this allowed me to sign in to admin.
I'm still working on the django-admin.py configuration question (which appears to not be related), and will reopen it in a separate question.

In my case, I had "SESSION_COOKIE_SECURE = True" because I was trying SSL configurations. Just commenting this line worked.

Well, to answer one of your questions, the parent folder of mysite is what needs to be on the python path. Adding mysite itself to your python path will expose the contents of mysite, not the mysite module to python.
As for runserver working / passwords not working, I haven't a clue. Have you customized anything with authentication?
Can you start the shell and try authenticating with your superuser?
http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate
You said in a comment that "is_staff was set appropriately... same problem." -- does that mean you were able to log in with the first superuser to set is_staff??

I ran into a similair problem but, I couldn't login on the admin interface even after removing the database and creating it from scratch. The problem was caused by a real silly mistake.
Don't run python manage.py testserver by accident. Make sure you use python manage.py runserver.

Related

Python Django set DJANGO_SETTINGS_MODULE when migrating source code

Task: Set up a new running environment by being provided python/Django source code and some additional details.
Problem: Cannot get Django-admin to validate due to missing/incorrect settings configuration
"django.core.exceptions.ImproperlyConfigured Requested setting USE_I18N, but settings are not configured. ..... "
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings
Env Details: Ubuntu OS, Python 2.7, Django 1.7, PostgreSQL (also Supervisor + gunicorn) Running a venv located **/home/dave/python-env/vas/**bin/activate
Python sys.path
/usr/lib/python2.7/* (multiple defined)
/home/dave/python-env/vas/python2.7/site-packages
So tried several methods (including #export DJANGO_SETTINGS_MODULE=project-name.settings....) with little success.
How can one set the DJANGO_SETTINGS_MODULE variable?
os.environ.setdefault() is set in wsgi (I know this is the next step)
BUT this value is also set in /manage.py ...?
os.environ.setdefault("DJANGO_SETTINGS_MODULE", project_name.settings)
The directory /var/www/app/ (where the python source code is located) has several files, one of them is the project_name where the settings.py sits....
I am new to python/django...
Trying to get django-admin.py validate to validate.
Update: Running #python manage.py runserver runs OK. #python manage.py validate|check returns "System check identified no issues (0 silenced)
Running #django-admin.py check returns the error in question. "You must either define DJANGO_SETTINGS_MODULE ...."
UPDATE 2: Solution
Turns out you don't need django-admin.py as suggested by (Alasdair) and you can use manage.py.
Details - If the 'manage.py check' function returns no issues and #pip install -r requirements.txt completes within your virtual environment THEN one can run #manage.py createsuperuser
I was able to use #manage.py runserver after creating a super user, and using this new user (as the database tables were empty for security reasons) I was able to log into 127.0.0.1:8000/admin. From there the models/tables were visible and using the admin functions I could create a new user + group to access the original system that was being migrated as an admin user.
Also not that a database was required (running postgres) with db/username/pass as per settings files and a git repository (at least empty initialised) for raven...
hope this helps someone coming into python.

My Django secret key is in an environment variable but I can't do syncdb

I'm setting up a Django project with different files for local and production settings. I can confirm that my Django secret key is successfully in an environment variable in virtualenv and when I do runserver I get no error. However when I try manage.py syncdb I get
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
I don't understand why I can successfully browse to the site after runserver but I can't sync the database. When I run env I can see that the secret key is there and in my base settings file (imported into local settings) I am doing this:
SECRET_KEY = os.environ.get('MY_SECRET_KEY')
Any help debugging this would be greatly appreciated.
Euan
I'm not sure why the runserver command is working while syncdb isn't, but you can sort it out by adding a environment variable for DJANGO_SETTINGS_MODULE in the same way you did for the SECRET_KEY. The only difference is that you don't need to reference DJANGO_SETTINGS_MODULE within the django code anywhere. I'm running my own setup in exactly that way and the only problem I run into is forgetting to change the settings module when I switch between projects :-)
EDIT: I didn't realise that you were adding --settings=myapp.settings.local to runserver as well as syncdb. The reason you need to do this is that you are using settings on a different path from the default so python can't find them. Also, although you set the DJANGO_SETTINGS_MODULE in the wsgi file, this is only fired when the site is accessed via your webserver. When running a manage command the wsgi file is ignored (AFAIK) so adding DJANGO_SETTINGS_MODULE to your environment variables in the same way as SECRET_KEY makes your settings file available to the manage command.
Hope that helps
Somewhat similar situation here, using virtualenv running an outer script which involves django models.
To make this work please make sure:
Your sys.path list has a path to your virtualenv site-packages. For me it's: sys.path.append('/home/user/.virtualenvs/Project/local/lib/python2.7/site-packages')
Your django settings variable is added to os.environ. Eg: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Project.settings")

django: cannot import settings, cannot login to admin, cannot change admin password

It seems that I am completely lost here. Yesterday I noticed that I cannot login to the admin panel (don't use it much, so it's been some weeks since last login). I thought that I might have changed the admin password and now I can't remember it (though I doubt it).
I tried django-admin.py changepassword (using django 1.2.1) but it said that 'changepassword' is unknown command (I have all the necessary imports in my settings.py. Admin interface used to work ok).
Then I gave a django-admin.py validate. Then the hell begun. django-admin.py validate gave me this error: Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I then gave a set DJANGO_SETTINGS_MODULE=myproject.settings
and then again a django-admin.py validate
This is what I get now: Error: Could not import settings 'myproject.settings' (Is it on sys.path? Does it have syntax errors?): No module named myproject.settings
and now I am lost. I tried django console and sys.path.append('c:\workspace') or sys.append('c:\workspace\myproject') but still get the same errors.
I use windows 7 and my project dir is c:\workspace. I don't use a PYTHONPATH variable (although I tried setting it temporarily to C:\workspace but I still get the same error). I don't use Apache, just the django development server.
What am I doing wrong? My web page works fine. I think that the fact that I can't login as admin is related to the previous import error, no?
PS: I also tried this: http://coderseye.com/2007/howto-reset-the-admin-password-in-django.html but still I couldn't change admin password for some reason. Although I could create another admin user (with which I couldn't login).
-EDIT- I forgot to mention that I use postgresql.
django-admin.py doesnot have changepassword option;
python manage.py does

Install django on webfaction

I am trying to install django on Webfaction, and i have one all the steps like in the tutorial here: http://wiki.osqa.net/display/docs/Installing+OSQA+on+WebFaction
But when i am trying to synchronyse the database, if i enter python2.5 manage.py syncdb --all i get: Usage: manage.py syncdb [options]
Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created.
manage.py: error: no such option: --all
if i enter manage.py syncdb i get Error: Could not import settings 'osqa.settings' (Is it on sys.path? Does it have syntax errors?): No module named settings
i have set all like in the tutorial...is a problem with the import line in settings.py : from settings_local import * as if i comment that i get an error from if DEBUG line... Where am i wrong??? all the files have the same rights - 617 and i cannot change the rights, also i don;t think is a problem with rights. thanks!
Try enabling DEBUG in the settings file to see if you get any further indication of the failure. There is a path problem somewhere. You could also try running python in the osqa directory and enter import settings_local to see what you get.

Unable to start a django server in my computer

I exported the path of my django project by
$ export DJANGO_SETTINGS_MODULE=/Users/masi/Documents/Test/djangobook/ch3.settings
I run unsuccessfully
$ django-admin.py runserver
Error: Could not import settings '/Users/masi/Documents/Test/djangobook/ch3.settings' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.
How can you start Django server without the error message?
Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make sure that the ch3 app is in your $PYTHONPATH, too.
For example, if your app is at /Users/masi/Documents/Test/djangobook/, then set $DJANGO_SETTINGS_MODULE to ch3.settings, and make sure your $PYTHONPATH includes /Users/masi/Documents/Test/djangobook.
$ export PYTHONPATH=/Users/masi/Documents/Test/djangobook/
$ export DJANGO_SETTINGS_MODULE=ch3.settings
From the django docs on django-admin.py and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks.
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py
So, if your directory structure looks like:
djangobook/
ch3/
settings.py
Do the following and you can ignore all DJANGO environment variables (unless you have some really weird install):
$ cd /Users/masi/Documents/Test/djangobook/ch3/
$ python manage.py runserver
For those that come across the same error, when trying to run something similar:
python manage.py runserver --settings=settings_dev
When the settings file is within an app directory, like so:
mysite/
settings.py
settings_dev.py
requirements.txt
manage.py
You don't have to specify $PYTHONPATH (at least not four years on) you just need to make sure your --settings value contains the folder name — you also need to use dot notation, slashes will not do.
python manage.py runserver --settings=mysite.settings_dev
It is the same story when exporting a $DJANGO_SETTINGS_MODULE value:
export DJANGO_SETTINGS_MODULE=mysite.settings_dev
Might save someone else the time that I lost working that out.
You can also try manage.py.
From your project directory, run
$ python manage.py runserver
Even though it's just a wrapper, manage.py always works for me while django-admin.py doesn't. Obviously we're both doing something wrong (I just got started with Django), but this should get you going at least.