Good night, I'm having a big problem with django, no matter what I do, it always saves the data I set as the default rather than the data I sent through the form.
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
vol = models.IntegerField(default=0,validators=[MinValueValidator(0), MaxValueValidator(100)])
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py
from django.shortcuts import render, redirect
from .forms import UserForm, ProfileForm
from .models import User,Profile
#adicionar codigo
def create_profile(request):
userform = UserForm(request.POST or None)
profileform = ProfileForm(request.POST or None)
if userform.is_valid() and profileform.is_valid():
usk = userform.save()
profileform.instance.user = User.objects.get(id = usk.pk)
profileform.vol = request.POST.get('vol')
profileform.save()
return redirect('login')
return render(request, 'formprofile.html', {'userform': userform, 'profileform': profileform})
def delete_profile(request,codigo):
user = User.objects.get(id = codigo)
profile = Profile.objects.get(id = codigo)
if request.method == 'POST':
user.delete()
profile.delete()
return redirect('initial')
return redirect('initial') #alterar depois
def update_profile(request, codigo):
user = User.objects.get(id = codigo)
profile = Profile.objects.get(id = codigo)
userform = UserForm(request.POST or None, instance = user)
profileform = ProfileForm(request.POST or None, instance = profile)
if userform.is_valid() and profileform.is_valid():
userform.save()
profileform.save()
return redirect('ger_profile')
return render(request, 'formprofile.html', {'userform':userform, 'profileform':profileform})
def ger_profile(request, codigo):
user = User.objects.get(id = codigo)
return render(request, 'gerprofile.html', {'user':user})
forms.py
from django import forms
from .models import Profile,User
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = [
"username",
"password",
"email",
]
password = forms.CharField(widget=forms.PasswordInput)
widgets = {
'password': forms.PasswordInput(),
}
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"vol",
]
registration template
{% extends 'basep.html' %}
{% block content %}
<h1>Criar Conta de Usuário</h1>
<form method="POST" id="fprofile">
{% csrf_token %}
{{ userform.as_p }}
{{ profileform.as_p }}
<button type="submit">Cadastrar</button>
</form>
<script type="text/javascript">
it = document.getElementById('id_vol');
it.setAttribute("min","0");
it.setAttribute("max","100");
</script>
{% endblock %}
base template:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
What I want is to simply set up a registration system, where users register their audio data in addition to their data, it is not very complicated, the big problem as I said is in views.py
, that simply does not work correctly no matter what I do, the create_profile
method does not save the POST volume of the form, it is always the default
value, if anyone can help me solve this problem, I would appreciate it.
NOTE: I'm sorry for my Portuguese because I'm wrong, I'm writing this late at night, since I spent almost all night trying to solve this stone in the shoe, Thank you very much anyone who can help me.