Commit ad19adf3 authored by thiago's avatar thiago

usuario

parent b632ac0b
......@@ -6,5 +6,15 @@
</head>
<body>
{{ mensagem }}
{% if form.instance.id %}
<form action="{% url 'edit-usuario' form.instance.id %}" method="post">
{% else %}
<form action="{% url 'cad-usuario' %}" method="post">
{% endif %}
{% csrf_token %}
{{ form.as_p }}
<button type="submit">salvar</button>
</form>
</body>
</html>
\ No newline at end of file
......@@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django.conf.urls import include, url
from appagenda.views.telefone import CadTelefone, ListaTelefone
from appagenda.views.usuario import ConsultaUsuario
from appagenda.views.usuario import ConsultaUsuario,CadUsuario
urlpatterns = [
......@@ -11,5 +11,6 @@ urlpatterns = [
url(r'^edita-telefone/(?P<id>\d+)/$',CadTelefone.as_view(), name='edit-telefone'),
url(r'^lista-telefone/$', ListaTelefone.as_view(), name='lista-telefone'),
url(r'^lista-usuario/$', ConsultaUsuario.as_view(), name='lista-usuario'),
url(r'^cadastro-usuario/$', CadUsuario.as_view(), name='cad-usuario'),
url(r'^edita-usuario/(?P<id>\d+)/$', CadUsuario.as_view(), name='edit-usuario'),
]
\ No newline at end of file
......@@ -12,3 +12,30 @@ class ConsultaUsuario(View):
def get(self, request):
usuarios = Usuario.objects.all()
return render(request, self.template, {"usuarios": usuarios})
class CadUsuario(View):
template="usuario/salvar.html"
def get(self,request,id=None):
if id:
usuario = Usuario.objects.get(id=id)
form = UsuarioForm(instance=usuario)
else:
form = UsuarioForm()
return render(request,self.template,{"form":form})
def post(self,request,id=None):
form =UsuarioForm(request.POST)
if id:
usuario = Usuario.objects.get(id=id)
form = UsuarioForm(data=request.POST,instance=usuario)
if form.is_valid():
form.save()
form = UsuarioForm()
mensagem ="cadastardo com sucesso!"
else:
mensagem ="Formulario não valido"
# form=UsuarioForm()
return render(request,self.template,{"form":form,"mensagem":mensagem })
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment