我们使用Django在Heroku上启动了一个项目,但是用户并没有持久化在我们数据库的Django User表上,而是作为Django的Admin用户。
我们从django.contrib.auth.models.User中使用User.objects.create_user()
数据库为MySQL
有什么建议吗?
更新1(2023-04-04)
一些代码片段
- views.py*
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
...code...
def insertUser(request):
...code...
# Username validation
if User.objects.filter(username=request.POST['name']).exists():
data['msg'] = 'Usuário já cadastrado!'
data['class'] = 'alert-danger'
return render(request, 'cadUser.html', data)
# Email validation
if User.objects.filter(email=request.POST['email']).exists():
data['msg'] = 'Email já cadastrado!'
data['class'] = 'alert-danger'
return render(request, 'cadUser.html', data)
# Validação de Registro funcional
# New user recording
user = User.objects.create_user(request.POST['name'], request.POST['email'], request.POST['password'])
Docente.reg_funcional = request.POST['reg_funcional']
#user.reg_funcional = request.POST['reg_funcional']
user.save()
data['msg'] = 'Usuário cadastrado com sucesso!'
data['class'] = 'alert-success'
return render(request, 'loginUser.html', data)
- 设置.py*
from pathlib import Path
import django_on_heroku
...settings...
INSTALLED_APPS = [
<our stuff>,
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_simple_cookie_consent',
]
...settings...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
<connection stuff>
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
...settings...
django_on_heroku.settings(locals())
1条答案
按热度按时间pdkcd3nj1#
您正在使用
django-on-heroku
。这个库开箱即用的功能之一是用
DATABASE_URL
环境变量中的任何内容覆盖数据库连接参数。很可能您手动设置的MySQL参数没有被使用。您可以通过向
django_on_heroku.settings()
传递关键字参数来禁用功能,例如:这应该可以防止
django_on_heroku
修改您的settings.DATABASES
。我建议你也检查一下你设置的配置变量和插件。如果你正在为Heroku Postgres或其他你不需要的数据库插件付费,你可能会想删除它。