Reputation: 1
I'm trying to understand why my code isn't work fine.
This is the models:
class Usuario(models.Model):
user = models.OneToOneField(User,default=None)
nombre = models.CharField(max_length=100)
correo = models.CharField(max_length=300)
tel = models.CharField(max_length=100)
cp = models.CharField(max_length=100)
def __unicode__(self):
return self.nombre
This is the view:
def RegistrosUsuarios(request):
if request.method == "POST":
nombre = request.POST['nombre']
correo = request.POST['correo']
contrasena = request.POST['contrasena']
mailused = None
try:
mailused = User.objects.get(email=correo)
except User.DoesNotExist:
print("Usuario no existe")
if mailused is None:
user = User.objects.create_user(username=correo, email=correo)
user.set_password(contrasena)
user.save()
ultimoUser = User.objects.all().last()
usuario = Usuario(user=ultimoUser,nombre=nombre,correo=correo)
usuario.save()
user = authenticate(username=user.username,password=contrasena)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/')
else:
return HttpResponse("inactive user")
else:
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/')
The form:
<form action="/RegistrosUsuarios/" method="post" enctype="multipart/form-data" autocomplete="off">
{% csrf_token %}
<div class="modal-body">
<div class="md-form">
<input maxlength="500" name="nombre" type="text" placeholder="Nombre" required="required" style="width: 25%;">
<label for="nombre" style="color: #000!important;display: none;" class="active"></label>
</div>
<div class="md-form">
<input type="email" name="correo" id="correo" required="required" placeholder="Correo" style="width: 25%;">
<label for="correo" style="color: #000!important;display: none;" class="active">Email</label>
</div>
<div class="md-form">
<input type="password" name="contrasena" id="contrasena" required="required" placeholder="Contraseña" style="width: 25%;">
<label for="contrasena" style="color: #000!important;display: none;" class="active">Contraseña</label>
</div>
<div class="input-field" style="border-top:0px ;align:center;" align="center">
<button type="submit" name="action" class="btn waves-effect waves-light" style="border: 2px solid #000;color: #000!important; width: 15%;background-color:transparent!important;font-family: Geomanist-Regular;letter-spacing: 0px;font-size: 1.1rem; ">REGISTRARSE</button>
</div>
</div>
</form>
the url:
urlpatterns = [
url(r'^$', views.Index),
...
url(r'^RegistrosUsuarios/$', views.RegistrosUsuarios),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
But, when I try to create an object from Usuario model:
TypeError at /RegistrosUsuarios/
Usuario() got an unexpected keyword argument 'user'
Request Method: POST
Request URL: http://127.0.0.1:8000/RegistrosUsuarios/
Django Version: 1.11
Exception Type: TypeError
Exception Value:
Usuario() got an unexpected keyword argument 'user'
Upvotes: 0
Views: 5963
Reputation: 59164
It looks like you have a method named Usuario
and it is imported together with the Usuario
model class.
You can also use the .create()
method of the default manager to create objects instead of instantiating and saving them manually:
Usuario.objects.create(user=..., ...)
This way you will not need to call the .save()
method as it will be automatically saved.
As a side note, it is recommended to follow the PEP 8 naming conventions and name your methods in lowercase_letters
and use CamelCase
for your classes. This will reduce the number of errors of this type.
Upvotes: 1