Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Registration with error: no such table: auth_user

I am trying to use Django's default Auth to handle register and log in.

setting.py:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'books',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTH_USER_MODEL = 'books.User'

books.models.py:

class User(AbstractUser):
    account_balance = models.DecimalField(max_digits=5, decimal_places=2, default=0)

views.py:

from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/accounts/profile/")
    else:
        form = UserCreationForm()
    return render(request, "registration/register.html", {'form': form,})

urls.py:

urlpatterns = patterns('',
    (r'^accounts/login/$', login),
    (r'^accounts/logout/$', logout),
    (r'^accounts/profile/$', profile),
    (r'^accounts/register/$', register),
)

I tried deleting the db.sqlite3 file and re-ran python manage.py syncdb but I still get this error message:

OperationalError at /accounts/register/
no such table: auth_user
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/register/
Django Version: 1.7b4
Exception Type: OperationalError
Exception Value:    
no such table: auth_user
like image 814
user2988464 Avatar asked Jul 10 '14 16:07

user2988464


4 Answers

./manage.py migrate

If you've just enabled all the middlewares etc this will run each migration and add the missing tables.

like image 91
jmoz Avatar answered Oct 19 '22 13:10

jmoz


Only thing you need to do is :

python manage.py migrate

and after that:

python manage.py createsuperuser

after that you can select username and password.

here is the sample output:

Username (leave blank to use 'hp'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
like image 28
Rajiv Sharma Avatar answered Oct 19 '22 12:10

Rajiv Sharma


Update

You are probably getting this error because you are using UserCreationForm modelform, in which in META it contains User(django.contrib.auth.models > User) as model.

class Meta:
    model = User
    fields = ("username",)

And here you are using your own custom auth model, so tables related to User has not been created. So here you have to use your own custom modelform. where in Meta class, model should be your User(books.User) model

like image 19
ruddra Avatar answered Oct 19 '22 12:10

ruddra


This will work for django version <1.7:

Initialize the tables with the command

manage.py syncdb

This allows you to nominate a "super user" as well as initializing any tables.

like image 12
holdenweb Avatar answered Oct 19 '22 12:10

holdenweb