Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
El método POST no recibe ninguna información del formulario (DJANGO & BOOTSTRAP)

Tengo un formulario que registra un nuevo usuario, usé Bootstrap 5 para que se vea bien. Sin embargo, cuando presiono el botón de enviar, el formulario no se valida. El error que muestra cuando escribo print(form.errors) dice que todos los campos son obligatorios, lo que significa que no recibió nada. ¿Por qué pasó esto? Este es mi código:

HTML

 <div class="card" id='signup_card'> <div class='card-header'> <h2 class='card-title'>Sign Up</h2> </div> <div class='card-body'> <form method='POST' action="" id='create_user_form' style="color: #fff;"> {% csrf_token %} <label for='usernameInput' class="form-label">{{form.username.label}}:</label> <input type="text" id='usernameInput' style="margin-bottom: 20px;"> <label for='phoneNumberInput' class="form-label">{{form.phone_number.label}}:</label> <input type="text" id='phoneNumberInput' style="margin-bottom: 20px;"> <label for='emailInput' class="form-label">{{form.email.label}}:</label> <input type="text" id='emailInput' style="margin-bottom: 20px;"> <label for='password1Input' class="form-label">{{form.password1.label}}:</label> <input type="text" id='password1Input' style="margin-bottom: 20px;"> <label for='password2Input' class="form-label">{{form.password2.label}}:</label> <input type="text" id='password2Input' style="margin-bottom: 20px;"> <button class="btn btn-primary" type='submit'>Submit</button> </form> </div>

modelos.py

 class Account(User): phone_number = BigIntegerField()

formularios.py

 class CreateAccountForm(UserCreationForm): class Meta: model = Account fields = ['username', 'email', 'phone_number', 'password1', 'password2']

vistas.py

 def signup(request): if request.method == 'GET': form = CreateAccountForm() ctx = { 'form':form } return render(request, 'signup.html', ctx) if request.method == 'POST': form = CreateAccountForm(request.POST) if form.is_valid(): form.save() else: print(form.errors) return render(request, 'index.html')
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Sus elementos de entrada deben especificar un atributo name="…" para especificar a qué nombre asociarán un valor:

 <form method='POST' action="" id='create_user_form' style="color: #fff;"> <label for='usernameInput' class="form-label">{{form.username.label}}:</label> <input type="text" name="username" id='usernameInput' style="margin-bottom: 20px;"> <label for='phoneNumberInput' class="form-label">{{form.phone_number.label}}:</label> <input type="text" name="phone_number" id='phoneNumberInput' style="margin-bottom: 20px;"> <label for='emailInput' class="form-label">{{form.email.label}}:</label> <input type="text" name="email" id='emailInput' style="margin-bottom: 20px;"> <label for='password1Input' class="form-label">{{form.password1.label}}:</label> <input type="text" name="password1" id='password1Input' style="margin-bottom: 20px;"> <label for='password2Input' class="form-label">{{form.password2.label}}:</label> <input type="text" name="password2" id='password2Input' style="margin-bottom: 20px;"> <button class="btn btn-primary" type='submit'>Submit</button> </form>
over 4 years ago · Santiago Trujillo Report

0

Su error es normal porque necesita agregar un atributo de nombre al campo de entrada :

 <label for='usernameInput' class="form-label">{{form.username.label}}:</label> <input type="text" id='usernameInput' name='username' style="margin-bottom: 20px;">

También puede usar el formulario completo de Django de esta manera:

 <label for='usernameInput' class="form-label">{{form.username.label}}:</label> {{ form.username }}

Ahora , si desea agregar un nombre de clase boostrap para tener un aspecto agradable, puede anular el método __init__ de la clase Form de esta manera:

 class CreateAccountForm(UserCreationForm): class Meta: model = Account fields = ['username', 'email', 'phone_number', 'password1', 'password2'] # Here we override the form elements attributes to add boostrap class def __init__(self, *args, **kwargs): super(RegistrationForm, self).__init__(*args, **kwargs) # Add some placeholder to username input field self.fields['username'].widget.attrs['placeholder'] = 'Your username' # add an id to username input field self.fields['username'].widget.attrs['id'] = 'usernameInput' # add the same class to all the fields (commom attribute value for all) for field in self.fields: self.fields[field].widget.attrs['class'] = 'form-control'

Más sobre el formulario del modelo de Django aquí

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!