1

I have tried many way to solve this, but none of them work. My question is when I tried to register a user, it shows an error no such table: auth_user . (py2.7 django 1.10)

I am using a custom user (because I want to add locations(one user can have multiple locations) to my users)

in my models.py:

from __future__ import unicode_literals
from django.conf import settings
from django.db import models
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model

# Create your models here.
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

class Locations(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    lat = models.FloatField(default=0.0)
    lng = models.FloatField(default=0.0)

class UserForm(UserCreationForm):

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

In my setting.py:

AUTH_USER_MODEL = 'weather.User'

# Application definition

INSTALLED_APPS = [
    'weather',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

in my views.py:

    from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.views.generic.edit import CreateView
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render_to_response 
from django.http import HttpResponseRedirect 
from django.template.context_processors import csrf


def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('register/complete')

    else:
        form = UserCreationForm()
    token = {}
    token.update(csrf(request))
    token['form'] = form

    return render_to_response('registration/register.html', token)


def registration_complete(request):
    return render_to_response('registration/registration_complete.html')

I have tried

python manage.py makemigrations myapp
python manage.py migrate auth
python manage.py migrate 

none of them helps, all same error.

Thanks!

KKlalala
  • 965
  • 1
  • 13
  • 23

3 Answers3

1

It might be for few reasons, like

  • Add your weather application in list of INSTALLED_APPS after all default apps, from django.contrib.*

  • Try to use in your UserForm user model in same way as in Locations model:

Like this

from django.contrib.auth import get_user_model
class UserForm(UserCreationForm):

class Meta:
    model = get_user_model()
    fields = ("username",)
s_mart
  • 735
  • 7
  • 21
  • I tried to put weather after django.contrib.*, it does not help – KKlalala Feb 16 '17 at 21:04
  • When I tried the second bullet point, it gives:AttributeError: 'str' object has no attribute '_meta' – KKlalala Feb 16 '17 at 21:06
  • @FrancicaZhao sorry, my fault. I've forgot that `settings.AUTH_USER_MODEL` returns just string - the name of model. You should try to use function `get_user_model()`. I update answer – s_mart Feb 17 '17 at 01:24
1

Try this:

python manage.py makemigrations YOURAPPNAME
python manage.py migrate

The idea is to make migrations for a specific app, as some migrations might not get applied when performing makemigrations on the whole project.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
Mandeep Bagga
  • 393
  • 4
  • 11
0

1)python manage.py createsuperuser

2)python manage.py makemigrations OR python manage.py migrate

3)python manage.py runserver

Hopefully it may work