Send form Django

Asked

Viewed 563 times

0

I’m new to python and I’m trying to register, when I click to send nothing happens, no post

py views.

    from django.http import HttpResponse
    from django.shortcuts import render
    from portal.cadastro.forms import CadastroForm
    from django.shortcuts import redirect
    from models import *

    def cadastro(request):
    if request.method == 'POST':
        return create(request)
    else:
        return new(request)


def new(request):
    return render(request, 'cadastro1.html',
                {'form': CadastroForm()})


def create(request):
    form = CadastroForm(request.POST)
    if not form.is_valid():
        return render(request, 'cadastro1.html',
                    {'form': form})
    obj = form.save()
    return HttpResponseRedirect('/cadastro1')

cadastro1.html

 <div class="row">
     <div class="col s12">
<form action="." method="POST">

<div class="card white darken-1">
    <div class="card-content black-text">
 <p>
<label for="{{ form.email.id_for_label }}">{{ form.email.label }}:</label>
{{ form.email }}
{{ form.email.errors }}
</p>

<p>
<label for="{{ form.nome.id_for_label }}">{{ form.nome.label }}:</label>
{{ form.nome }}
{{ form.nome.errors }}
</p>
<p>

 <label for="{{ form.divisao.id_for_label }}">{{ form.divisao.label }}:</label>
  <select class="browser-default">
    <option value= {{form.divisao}}>{{form.divisao}}</option>
  </select>
{{ form.divisao }}
{{ form.divisao.errors }}
</p>

<p>
<label for="{{ form.vinculo.id_for_label }}">{{ form.vinculo.label }}:</label>
    <select class="browser-default">
        <option value={{form.vinculo}}>{{form.vinculo}}</option>
          </select>
</p>

<button class="btn waves-effect waves-light" type="submit" name="enviar">Enviar
    <i class="material-icons right" >send</i>
  </button>
</div>
    </div>
    </form>
    </div>

</div>

py.models

DIVISOES = (
    (u'SSS', u'SSS'),
    (u'Banco de Dados', u'Banco de Dados'),
    (u'Redes', u'Redes'),
    (u'Outros', u'Outros'),
    )


VINCULOS = (
    (u'Bolsista', u'Bolsista'),
    (u'Estagiário', u'Estagiário'),
    (u'Terceiro', u'Terceiro'),
    (u'Servidor', u'Servidor'),
    )




# Create your models here.
class Usuario(models.Model):
    email = models.EmailField(blank=True)
    nome = models.CharField(max_length=255)
    divisao = models.CharField(max_length=255, choices=DIVISOES)
    vinculo = models.CharField(max_length=255, choices=VINCULOS)

    def __unicode__(self):
        return self.email

    class Meta:
        verbose_name = u'Usuário'
        verbose_name_plural = u'Usuários'

Forms.py

DIVISOES = (
    (u'SSS', u'SSS'),
    (u'Banco de Dados', u'Banco de Dados'),
    (u'Redes', u'Redes'),
    (u'Outros', u'Outros'),
    )


VINCULOS = (
    (u'Bolsista', u'Bolsista'),
    (u'Estagiário', u'Estagiário'),
    (u'Terceiro', u'Terceiro'),
    (u'Servidor', u'Servidor'),
    )


class CadastroForm(forms.Form):
    email = forms.EmailField(label='Email')
    nome = forms.CharField(label='Nome')
    divisao = forms.ChoiceField(widget=forms.Select, choices=DIVISOES, label='Divisão')
    vinculo = forms.ChoiceField(widget=forms.Select, choices=VINCULOS, label='Vinculo')

class CadastroForm(forms.ModelForm):
    class Meta:
        model = Usuario
        fields = '__all__'
  • Congratulations on changing the select fields to ChoiceField now consider at least giving the acceptance in the answer that guided you to this, so who knows who the author might consider considering this question. :-)

  • Sidon, I decided to add the "I’m new in python" just to show that I have little knowledge, I’ve already given the accepted and if you want to help me, you talked about showing the rendering part, this is not the view? I have to put some code in the view to get the information from Choice? Because it’s not saving... I click to send and nothing happens

  • I try to help, yes, but let’s continue on the original question, while I will analyze this.

  • I could not understand in this your code (this question) the following: Where in the second view (registration) you are trying to access the email that was typed in the first (registration) in which part of the code you try or want to access this information? Another strange thing is that in forms.py you have 2 Forms with the same name, this can not. :-)

  • I made some modifications here and updated the question, because it is really not necessary to have this separate email, I put now on the same page, you know tell me what is wrong for not submitting the form?

  • Vc continues with 2 Forms with the same name (this can’t), your editor is not 'warning'? (see forms.py), One is from the Form class and another Modelform, which one you want to use in the view? Another thing: Try to correct the aesthetics of the code in the question, see that there is a button in the edition that already 'pushes' the code 4 spaces and so leave formatted, avoiding that you have to keep doing it manually

Show 1 more comment

1 answer

-1

your form is pointing to the root of the site. change this:

<form action="." method="POST">

for this reason:

<form method="POST">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.