I recently studied Django, but I came across a problem in creating forms, I tried to go in the official documentation of the Framework but I did not find the solution.
forms.py
from django import forms
from .models import Novo_Usuario
class FormNovoUsuario(forms.Form):
nome = forms.CharField(max_length=200, label='nome')
sobrenome = forms.CharField(max_length=200, label='sobrenome')
senha = forms.CharField(max_length=50, label='senha')
class Meta:
fields = ('nome', 'sobrenome', 'senha')
models = Novo_Usuario
models.py
from __future__ import unicode_literals
from django.db import models
class Novo_Usuario(models.Model):
nome = models.CharField(max_length=200)
sobrenome = models.CharField(max_length=200)
senha = models.CharField(max_length=50)
def __str__(self):
return self.nome
views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from .models import Novo_Usuario
from .forms import FormNovoUsuario
def cms(request):
return render(request, 'base.html')
def cad_novo_usuario(request):
form = FormNovoUsuario(request.POST or None)
if request.method == 'POST':
return form.save()
return render(request, 'novo_usuario.html', {'form': form})