-2
I’m doing a project with Django and using postgresql as a database. I created an app Accounts for user registration, but is not registered in the database and does not appear in the Django admin Follow the code snippets. If you can help, I’d really appreciate it!!
Another issue also, when I made a user by shell he created normal...
Accounts
-Views
def add_user(request):
template_name = 'accounts/add_user.html'
context = {}
if request.method == 'POST':
form = UserForm(request.POST)
if form.is_valid():
f = form.save(commit=False)
f.set_password(f.password)
f.save()
messages.success(request, "Usuário salvo com sucesso!")
form = UserForm()
context ['form'] = form
return render(request, template_name, context)
-Models
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
cell_phone = models.CharField('Celular', max_length=16)
class Meta:
verbose_name = 'Perfil do Usuário'
def __str__(self):
return self.user.username
-Forms
from django import forms
from django.contrib.auth.models import User
from .models import UserProfile
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = {'first_name', 'last_name', 'username', 'email', 'password'}
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ['user']
DB Settings in Settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DB_NAME', 'projeto4db'),
'USER': os.environ.get('DB_USER', '...'),
'PASSWORD': os.environ.get('DB_PASS', '...'),
'HOST': 'localhost',
'PORT': '5432',
}
}
Add User Template
{% load static %}
{% load widget_tweaks %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cadastrar</title>
<!-- Font Icon -->
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<!-- Main css -->
<link rel="stylesheet" href="{% static 'css/adicionar.css' %}">
</head>
<body>
<div class="main">
<div class="container">
<div class="sign-up-content">
<form method="POST" action="." class="signup-form">
{% csrf_token %}
<h2 class="form-group">Cadastre-se</h2>
<div class="form-group">
<label for="{{ form.first_name.id_for_label }}">Nome</label>
{{ form.first_name|add_class:"form-control"|attr:"autofocus" }}
</div>
<div class="form-group">
<label for="{{ form.last_name.id_for_label }}">Sobrenome</label>
{{ form.last_name|add_class:"form-control" }}
</div>
<div class="form-group">
<label for="{{ form.username.id_for_label }}">Usuário</label>
{{ form.username|add_class:"form-control" }}
</div>
<div class="form-group">
<label for="{{ form.email.id_for_label }}">E-mail</label>
{{ form.email|add_class:"form-control" }}
</div>
<div class="form-group">
<label for="{{ form.password.id_for_label }}">Senha</label>
{{ form.password|add_class:"form-control"|attr:"type:password" }}
</div>
<div class="form-textbox">
<button type="submit" class="submit">Cadastrar usuário</button>
</div>
</form>
<p class="loginhere">
Já tem uma conta?<a href="{% url 'accounts:user_login' %}" class="loginhere-link"> Entrar </a>
</p>
</div>
</div>
</div>
<!-- JS -->
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/jquery-3.5.1.min.js' %}"></script>
</body><!-- This templates was made by Colorlib (https://colorlib.com) -->
</html>
ola, did any test saving in sqlite ? will that is a problem in db drive.
– EiAlex
opa, I returned to sqlite (default of Django) and still the same thing, not saving you. I created another project from scratch with sql and not saved
– Franco Motta
I went to reread the topic and you said " and it doesn’t appear on Django admin " did you happen to remember to run the makemigrations and migrate ? for the EX model: python Manage.py makemigrations --name changed_my_model your_app_label
– EiAlex
I had already done it too, but nothing has changed... I did it again and it goes on
– Franco Motta