/register/处的 django-OperationalError 没有这样的表:auth_user [英] django-OperationalError at /register/ no such table: auth_user

查看:77
本文介绍了/register/处的 django-OperationalError 没有这样的表:auth_user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法来解决这个问题,但都没有奏效.我的问题是当我尝试注册用户时,它显示错误 no such table: auth_user .(py2.7 Django 1.10)

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)

在我的models.py中:

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",)

在我的 setting.py 中:

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',
]

在我的 views.py 中:

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')

我试过了

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

他们都没有帮助,都是同样的错误.

none of them helps, all same error.

谢谢!

推荐答案

可能有几个原因,比如

It might be for few reasons, like

  • INSTALLED_APPS 列表中添加您的 weather 应用程序所有默认应用程序,来自 django.contrib.*

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

尝试在您的 UserForm 用户模型中使用与Locations 模型:

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

像这样

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

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

这篇关于/register/处的 django-OperationalError 没有这样的表:auth_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆