Why "no such table: django_session" when trying to use admin? - django

I am pretty sure that database has been created and I have executed python manage.py syncdb properly but still i encounter this error when trying to access http://127.0.0.1:8000/admin/. Here are some details.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Exception Type: DatabaseError at /admin/
Exception Value: no such table: django_session

path to sqlite is usually the problem.
'NAME': 'c:/path/to/sqlite.db'

Make migrations will solve the problem.
Run below commands:
python ./manage.py migrate
python ./manage.py makemigrations

Try this
from os.path import dirname, abspath
ROOT = dirname(abspath(__file__)).replace('\\', '/') + '/'
print "self.__name__: " + __name__
print "self.__file__: " + __file__
print "ROOT: " + ROOT
import django
print "django.__path__: "
print (django.__path__)
# Contact for error messages etc. - dont forget the ',' after the first entry
ADMINS = (('dev', 'dev#example.com'),)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ROOT + 'project.db',
'USER': '', 'PASSWORD': '',
'HOST': '', 'PORT': '',
}
}
and check if you need + '/' + before the database name for your operating system.

SESSION_ENGINE it's using django.contrib.sessions.backends.db by default and that's make you need to :
put django.contrib.sessions in INSTALLED_APPS list variable in settings.py
you must migrate your database
if you don't want to do that, just put SESSION_ENGINE to django.contrib.sessions.backends.cache in your settings.py.
so, in your settings.py like this :
...
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
...
by the way, as in the documentation say :
'..the simple cache is faster because it disregards persistence.'
you can cek in this link

If you have added/updated table in any models.py file, you might need to migrate the db before running the server.
Run below commands before running 'python manage.py runserver':
python manage.py migrate
python manage.py makemigrations

That happens when you run the server without a database created or the session table inside of it (in case you recently add the admin app to your INSTALLED_APPS). In order to create the database or the table run this command.
python manage.py migrate
In older versions of django is this command
python manage.py syncdb

It prompted you to that error page because your Database wasn't even created, which lead to the absence of the mandatory tables to access the admin page.
Run the following two commands :
python3 manage.py migrate
python3 manage.py makemigrations
Running the first command actually creates your database which holds the necessary starter tables. I had the same problem and this solved 100% for me.
Solution source : #satya 's answer

Executing in the below order works fine:
First
python manage.py migrate
Then
python manage.py runserver.
Since we are applying all migrations before running the server.

Related

Django app not showing registered models in Admin page

I have tried everything I can think of and find on here but nothing seems to be able to get my models to register and show on my Django Admin page. Weird thing is I have another model that is already appearing on the Admin page that I've matched the syntax for but that one works and this other one does not.
models.py
class SimpleModel(models.Model):
something = models.CharField('Simple Model', max_length=200, default='foobar')
def __str__(self):
return self.name
admin.py
from .models import SimpleModel
admin.site.register(SimpleModel)
settings.py
INSTALLED_APPS = [
'bootstrap4',
'app.apps.RequestsDashboardConfig',
'applications_dashboard',
'requests_dashboard',
#'applications_dashboard.apps.ApplicationsDashboardConfig',
'requests_dashboard.apps.RequestsDashboardConfig',
#"app.requests_dashboard",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
You can see I've even tried multiple variations of the INSTALLED_APPS but even this still does not work. I don't even get any errors when I start the server with this series of commands after deleting the migrations.
python3 manage.py flush
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py runserver 0.0.0.0:80
I even tried deleting the entire DB, recreating it, and then running this again and still no luck!
Not sure where I went wrong in the end but for anyone who encounters similar issues to mine, I was able to solve this by doing another complete DB rebuild.

Django 1.6.5 user registration error no such table: auth_user

my settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'Mysite'
)
AUTH_USER_MODEL = 'Mysite.PortalUserAbstract'
my Mysite.models.py:
class PortalUserAbstract(AbstractUser):
is_client = models.BooleanField(default=False)
is_manager = models.BooleanField(default=False)
error message :
OperationalError at /accounts/signup/client/
no such table: auth_user
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/signup/client/
Django Version: 1.6.5
Exception Type: OperationalError
Exception Value:
no such table: auth_user
when i do manage.py syncdb it creates table "Mysite_portaluserabstract" but the auth module still looks for auth_user table.
what am I missing?
You're using Django 1.6, which is very out-of-date - it pre-dates migrations, which will create the missing tables for you. You should upgrade to at least Django 1.11 as was mentioned in comments; if this is a new project, I would recommend using Django 2.1 as of this writing, as the new URL syntax is much friendlier for getting started. It seems to be a new project as you are just starting with a custom user model.
In short:
Create a new virtualenv
pip install django
django-admin startproject myproject && cd myproject
python manage.py startapp mysite
edit mysite/models.py and add the following:
class PortalUserAbstract(AbstractUser):
is_client = models.BooleanField(default=False)
is_manager = models.BooleanField(default=False)
edit myproject/settings.py and add:
AUTH_USER_MODEL = 'Mysite.PortalUserAbstract'
run python manage.py makemigrations
run python manage.py migrate
Then you should be off and running with the latest version of Django. It would probably do you well to go through the tutorial: https://docs.djangoproject.com/en/2.1/intro/tutorial01/
Good luck!

Unable to login to django admin view with valid username and password

I know this question has been asked several times before but most of the question were asked long ago and old answers did not work for me.
I have a django-nonrel based app which is using dbindexer as backend and deployed on GAE. I am able to view homepage of my app which does not require login.
But when I try to login to admin view, it gives "wrong username / password"
On my local development server, if I use "manage.py runserver", then I am able to login on admin page. But If I run my app through GAE launcher, then I am not able to login.
I could gather that GAE launcher uses different django from "manage.py runserver".
So, how can I make GAE (on launcher as well as on deployment server) use django-nonrel?
Other details:
app.yaml does NOT include "django" library.
settings.py
DATABASES['native'] = DATABASES['default']
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'}
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djangotoolbox',
'autoload',
'dbindexer',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'djangoappengine',
'testapp',
)
urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
UPDATE 1::
As #dragonx pointed out, I need to run
python manage.py remote createsuperuser and create the user.
On local server, when I run 'manage.py syncdb', it fills database with initializing data which also includes creating a superuser. I use 'initial_data.yaml' inside 'fixtures' directory for this and is read automatically by syncdb command.
So, Is there any way to run "syncdb' on server side? Somehow I assumed this is happening automatically at deployment just like 'manage.py runserver' happens itself and I do not need to run app manually.
If I run manage.py remote syncdb, it blurts out following error:
google.appengine.api.datastore_errors.NeedIndexError: no matching index found.
<<ed>>some stack trace<<ed>>
The suggested index for this query is:
- kind: django_content_type
properties:
- name: app_label
- name: name
Update 2:
Instead of using appcfg.py update site command, if you use python manage.py deploy from your app directory, it runs fixtures on remote server. Don't know what's doing what.
manage.py remote loaddata initdata.yaml can also be used to initialize remote database.
But even after this, I still do not see the fixtures data loaded in admin interface i.e. it seems database was not initialized or maybe admin view is badly broken. But I'd save that for another question~
When you run python manage.py runserver it starts a local dev server on your local machine. It has it's own dev datastore on your local machine. At some point you created an admin user in your local database.
When you deploy on app engine, it runs your code on Google's servers. There's a datastore there, but it doesn't share the data on your dev server. You'll need to create an admin user in the production datastore too. Try:
python manage.py remote createsuperuser

django redistributable app with south

Django apps that are meant to be redistributed don't have a manage.py (nor a settings.py since that is part of the project, not the app).
How does one run schemamigration --auto in this scenario? Do I need to have a minimal settings/manage.py as part of the app repository in order to do this? Is there a way to do this with django-admin.py?
You just need to add the app to your Installed_Apps in your settings.py
Then you can run ./manage.py schemamigration <app_name> --auto
If the app doesn't have any migrations you will want to run ./manage.py schemamigration <app_name> --initial first and then ./manage.py schemamigration <app_name> --auto from then on.
Just managed to get this working in one of my project. Here's the code that works for me:
import sys
from django.conf import settings
from django.core.management import call_command
if not settings.configured:
settings.configure(
ROOT_URLCONF='',
DEBUG=False,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test.db'
}
},
INSTALLED_APPS=(
'south',
'my_app',
)
)
if __name__ == "__main__":
call_command('schemamigration', 'my_app',
initial=len(sys.argv) > 1,
auto=len(sys.argv) == 0
The above script is saved as migrate.py and run with python migrate.py or python migrate.py i (the i can be anything, and it will use --initial instead of --auto if present). Obviously, you can do fancier command line option parsin, but this works for me.
EDIT: Updated the script, DATABASES key was missing. On this project, I used the same database for testing the code, so it's not an entirely arbitrary configuration.

Django: MySQL no such table: aidata.django_session

I'm running Django 1.4 on Windows 7 in Pycharm and I installed WAMP because I need to have my data in a MySQL table.
This is from setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aidata',
'USER': 'root'
}
}
From installed_apps I uncommented:
'django.contrib.sessions'
Running manage.py syncdb does not create any tables ( even models) in my mysqldb.
I get the error when trying to acces /admin/
DatabaseError at /admin/
(1146, "Table 'aidata.django_session' doesn't exist")
Double check the db credentials
make sure you uncommented this line in your middleware:
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
)
then try to python manage.py syncdb.
if you are still having issues post any output
EDIT -- NEXT CHECK:
do you have a "django_content_type" table?
if so, does that table have a "session" record?
if so, delete the session record and try to python manage.py syncdb
EDIT -- STEP 3:
now i'm guessing, post up your settings file so i can make meaningful troubleshooting attempts
Stop your server if you have one running
go into your file browser and delete the settings.pyc file
try to python manage.py syncdb
my thought is that a pyc file with the sqlLite info may be cached and not regenerating
EDIT -- STEP 4:
everything in your settings.py look ok to me. try something for me? create a new django project, don't enable the admin or add in your apps i just want to know if from scratch everything in your django install seems to be working
django-admin.py startproject testsite
do the database configuration/setup
python manage.py syncdb
let me know if the models create properly
I was running into the same problem and for me (running django 1.7 development trunk of mid-sept.2013) it helped to
remove all south migrations ([app]/migration)-directories
remove south from INSTALLED_APPS in settings.py
That might be due to the shift towards the integrated migration system in django v1.7, but I'm speculating here.