Search code examples
pythonmysqldjangoheroku

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

Solution

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