django:用户注册错误:没有这样的表:auth_user [英] django: User Registration with error: no such table: auth_user
问题描述
我尝试使用Django的默认Auth来处理注册和登录。我认为这个程序是非常标准的,但是我的错误。
I try to use Django's default Auth to handle register and login. And I think the procedure is pretty standard, but mine is with sth wrong.
我的setting.py:
my 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'
我的书.models.py:
my books.models.py:
class User(AbstractUser):
account_balance = models.DecimalField(max_digits=5, decimal_places=2, default=0)
我的views.py:
my 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),
)
即使我尝试删除db.sqlit e3和re python manage.py syncdb
,仍然出现此错误消息:
Even I tried delete the db.sqlite3 and re python manage.py syncdb
, there's still 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
有人可以解释并告诉我应该做什么?
Can someone explain and tell me what I should do?
推荐答案
更新
您可能会收到此错误,因为您正在使用 UserCreationForm
modelform,其中 META
它包含用户
(django.contrib.auth.models> User)作为模型。
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",)
此处,您使用自己的自定义验证模型,因此尚未创建与 User
相关的表。所以在这里你必须使用自己的自定义模型。在Meta类中,模型应该是您的用户
(books.User)模型
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
这篇关于django:用户注册错误:没有这样的表:auth_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!