新用户在Heroku上的Django应用中不会在数据库中持久保存。

huangapple go评论79阅读模式
英文:

New users on Django app at heroku doesn't persist on database

问题

We started a project on Heroku, using Django, but users aren't persisted on the Django User table in our database, but as Admin users in Django?

我们在Heroku上开始了一个项目,使用Django,但是用户在我们的数据库中没有被保存在Django用户表中,而是作为Django中的管理员用户?

We use User.objects.create_user() from django.contrib.auth.models.User

我们使用来自django.contrib.auth.models.User的User.objects.create_user()。

Database is MySQL

数据库是MySQL

Any tips?

有什么建议吗?

英文:

We started a project on Heroku, using Django, but users aren't persisted on Django User table on our database, but as Admin users at Django?

We use User.objects.create_user() from django.contrib.auth.models.User

Database is MySQL

Any tips?

Update 1 (2023-04-04)

Some code snippets

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)

settings.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

得分: -1

你正在使用 django-on-heroku

这个库默认情况下会使用你的 DATABASE_URL 环境变量中的值来覆盖数据库连接参数,很可能你手动设置的MySQL参数不会被使用。

你可以通过向 django_on_heroku.settings() 传递关键字参数来禁用功能,例如:

django_on_heroku.settings(locals(), databases=False)

这将阻止 django_on_heroku 修改你的 settings.DATABASES

我建议你还要检查一下你设置的配置变量和插件。如果你在支付 Heroku Postgres 或其他不需要的数据库插件,你可能会想要移除它。

英文:

You are using django-on-heroku.

One of the things this library does out of the box is to override your datbabase connection parameters with whatever is in your DATABASE_URL environment variable. It is very likely that the MySQL parameters that you have set manually are not being used.

You can disable features by passing keyword arguments to django_on_heroku.settings(), e.g.:

django_on_heroku.settings(locals(), databases=False)

That should prevent django_on_heroku from modifying your settings.DATABASES.

I suggest you also review the config vars and addons that you have set. If you're paying for Heroku Postgres, or some other database addon that you don't need, you'll probably want to remove it.

huangapple
  • 本文由 发表于 2023年4月4日 08:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定