Django中用于创建注册页面的“没有这样的表”错误是啥?
Posted
技术标签:
【中文标题】Django中用于创建注册页面的“没有这样的表”错误是啥?【英文标题】:What is a 'no such table' error in Django for creating a register page?Django中用于创建注册页面的“没有这样的表”错误是什么? 【发布时间】:2021-08-24 00:24:38 【问题描述】:我正在尝试创建一个注册页面。我收到了这个错误。
views.py:
def login_view(request):
if request.method == "POST":
# Attempt to sign user in
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, username=username, password=password)
# Check if authentication successful
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/login.html",
"message": "Invalid username and/or password."
)
else:
return render(request, "auctions/login.html")
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse("index"))
def register(request):
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]
# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request, "auctions/register.html",
"message": "Passwords must match."
)
# Attempt to create new user
try:
print("momooo!")
user = User.objects.create_user(username, email, password)
print(("more mooomoo"))
user.save()
except IntegrityError:
return render(request, "auctions/register.html",
"message": "Username already taken."
)
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "auctions/register.html")
register.html 页面:
% extends "auctions/layout.html" %
% block body %
<h2>Register</h2>
% if message %
<div> message </div>
% endif %
<form action="% url 'register' %" method="post">
% csrf_token %
<div class="form-group">
<input class="form-control" autofocus type="text" name="username" placeholder="Username">
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" placeholder="Email Address">
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" placeholder="Password">
</div>
<div class="form-group">
<input class="form-control" type="password" name="confirmation" placeholder="Confirm Password">
</div>
<input class="btn btn-primary" type="submit" value="Register">
</form>
Already have an account? <a href="% url 'login' %">Log In here.</a>
% endblock %
models.py 内部:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
# pass
def create_user(username, email, password):
print("hello!!!!")
我在尝试注册新用户时收到此错误:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/register
Django Version: 3.2.4
Python Version: 3.9.5
Installed Applications:
['auctions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']
Traceback (most recent call last):
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
The above exception (no such table: auctions_user) was the direct cause of the following exception:
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Kaij\Documents\coding\cs50\commerce\auctions\views.py", line 55, in register
user = User.objects.create_user(username, email, password)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 152, in create_user
return self._create_user(username, email, password, **extra_fields)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\models.py", line 146, in _create_user
user.save(using=self._db)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /register
Exception Value: no such table: auctions_user
我对错误“没有这样的表:auctions_user”感到困惑。我还没有创建那个表,所以不确定他们为什么需要它或在哪里引用它。
【问题讨论】:
【参考方案1】:运行python manage.py makemigration
然后python manage.py migrate
创建用户表
【讨论】:
非常感谢@mkalioby。我实际上是这样做的,但是当我在 register.html 页面上注册时它似乎并没有改变问题我得到了同样的错误 你在设置文件中设置了AUTH_USER_MODEL
吗?
我现在不在电脑旁,但我非常感谢这个建议。我回来时要检查一下。再次感谢
我设置了AUTH_USER_MODEL = 'auctions.User'
。它仍然给我这个问题
设置后makemigrations并迁移【参考方案2】:
我发现有效的方法是运行
python manage.py makemigrations YOURAPPNAME
特定于我的应用 然后我也不得不
注释掉:
INSTALLED_APPS = [
...
#'django.contrib.admin',
...
]
并且还注释掉:
# path('admin/', admin.site.urls) in urls.py
那我终于可以跑了
python manage.py migrate
然后将它们添加回来[删除##以删除评论]
之后表格错误消失了。
这些以前的帖子有所帮助:
-
django-OperationalError at /register/ no such table: auth_user
django.db.migrations.exceptions.InconsistentMigrationHistory
【讨论】:
以上是关于Django中用于创建注册页面的“没有这样的表”错误是啥?的主要内容,如果未能解决你的问题,请参考以下文章