manage.py not updating database name - django

I have a Django application, and I recently changed the name of the database it is supposed to use. However, manage.py doesn't seem to be using the new database.
I've doublechecked the settings.py file, and I've even added a "print settings.DATABASE_NAME" to the manage.py file, and it prints out the correct name, but still connects to the old database.
For example, using ./manage.py dbshell:
NewDB
Password for user :
Welcome to psql 8.1.11, the PostgreSQL interactive terminal.
OldDB=>
So as far as I can see, it's completely ignoring what's in the settings file.
What could be causing this?

I figured out what the problem was.
While manage.py does take the settings file, the djang.conf.settings is loaded from the sys.path rather than what is passed via manage.py.
Apparently, the old location was in the sys.path, and so it was loading the old settings file.

Related

Deleted all my migrations including __init.py__

I am a beginner using Django. I have recently been working on a website that has a few different pages. I reference a couple of views from other apps in urls.py. One app I used to take inputs and make some calculations, so it had some forms defined. It was all working fine and I was able to import the views fine, but I changed one of my form field names. This caused some problems because I was storing these values in a database. That made sense to me because I had already saved some data with the previous naming convention and now I had changed it. So, I figured that deleting my database and migrations would allow me to start over and start a new database with my updated fields. I messed up though because I read that I should not have deleted my init.py file in my migrations folder.
I have tried re-doing my migrations with makemigrations and migrate but I keep getting an error saying that no changes are made and no new migrations show up in my folder. Also, now when I look in my urls.py file, the imported views and apps are showing up with a red underline and says "import package".
I have also tried reverting my code back and was able to get the migration files back, but the same error was shown in my urls.py file.
I have tried just about everything I could find online, but am now thinking that I may just have to recreate these apps.
Any help is appreciated!
First of all, to rename a form field, just change it in the models and then make and apply migrations. Django should detect the renaming of an existing field and create migrations.AlterField(...) statements (you can inspect the migration files that were created, and freely delete them if they are not yet applied/migrated).
First, please check if adding the empty file called __init__.py to the migration folder solves the problem. This is a python requirement, to let the interpreter know that the folder contains a python module.
Also if you have downloaded the project from somewhere, you need to have the existing migrations. Deleting them and recreating them yourself will likely fail.
After that just try running
python manage.py makemigrations
python manage.py migrate
If there are still no changes, check if there are migrations applied to your database by looking at the django_migrations table or by running this command:
python manage.py showmigrations
If this did not help, try to start with a clean project:
Backup all your code changes and revert to the starting point, the __init__.py file should be back and you should also delete the database (db.sqlite3)
run the migrate command
add all your code changes
check if your code changes have new migration files, they could cause problems. If you have not created or modified them manually, you can delete and let Django recreate them

Created a brand new Django Project, attempting to run the server generates an Improperly Configured Error

I have been working on a Django Project for a bit, until I took a break. One month later I dug back in to the project and went to run the server. I received an error:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I figured I must have tweaked something by accident. So I created a brand new Django Project (using a virtual environment), and just went to test the server. I received the same error. I tried the python manage.py shell solution listed in another answers but to no avail.
If it helps I'm on Linux with Django version 2.1.5 and Python 3.6.
Edit:
If anyone encounters something similar I found using python3 manage.py runserver works in place of using django-admin. Per Greg's answer below, I did begin to receive a new error ModuleNotFoundError: No Module named "mysite" exists. I will continue to search for an answer on that front.
Going off of the comments here.
If "env | grep DJANGO_SETTINGS_MODULE" returns empty, it means you have to set an environment variable stating where your settings.py file is located.
This can be done by doing the following:
export DJANGO_SETTINGS_MODULE=mysite.settings
Be sure to replace "mysite" with the name of your app!

Admin shows entries of a model even though database is empty

How is the following possible?
I cloned a django project into a dir, ran syncdb, check_permissions, migrate, createsuperuser and ran my development server. I then went to the admin and there I saw 2 entries of a model even though there are no fixtures that could cause these entries. When I looked at my sqlite database file, there are no entries for that model, even though it shows these two entries in the admin(I also did python manage.py dumpdata > my_db and case insensitively searched within that file, no result). It's also not browser related because the problem also occurs on a new installed browser.
These 2 entries are entries I made in the development server of the project on another location. But that server is not running and I'm using a different port.
I also grepped (grep -r -n entry_name) from my root folder looking for the names of those entries, however without result.
When I select these entries in the admin and click delete, nothing happens. These two entries remain there.
Edit 1
#Hedde:
I did ps ffaux|grep python . There is only one Django process running: /home/my_username/.virtualenvs/project_name/bin/python manage.py runserver 8044
I gave each of my settings files a print line to identify when each is running. Only the development settings is run, like expected and has always been the case.
I am using sqlite3 for the database, there is only one my_database.db file in my project dir. (find . -iname *db* only returns one database file)
Edit 2
When I clone the project from another pc and do the whole procedure listed above(syncdb, check_permissions, migrate, createsuperuser and run development server) everything works as expected, the entries are gone.
The problem was related to Redis. Simply flushing the Redis-db solved the problem.
$ redis-cli
$redis> flushall
$redis> exit

syncing sqlite3 for Django tutorial

I'm starting the Django tutorials and have virtual environments and django installed. I'm working on a Mac 10.6.8, which has sqlite3 already installed.
I'm working inside a virtual environment. I changed the settings.py file to:
ENGINE -- 'django.db.backends.sqlite3'
I left the NAME -- in the settings.py file blank (i.e. '') because the tutorial said
"If the file doesn't exist, it will automatically be created when you synchronize the database for the first time."
But when I run python manage.py syncdb, I get the following error:
django.core.exceptions.ImproperlyConfigured: Please fill out the database NAME in the settings module before using the database.
Why isn't my file automatically created? Or what do I need to do to solve this problem?
Thank you!
It will be created for you, but you have to specify what name to call it so just set NAME to sqlite.db or something similar. You can also specify a path so that it doesn't clog up your project directory. I usually save the dev database outside of my project.

Can't sign in to Django admin

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.