Após concluir o exercício 11.1, meu registro.py é exibido normalmente para cadastro, porem não consigo acessar mais http://localhost:8000/, é lançado o erro demonstrado abaixo.
Também não consigo cadastrar nenhum usuario no meu registro.py ele sempre retorna dados inválidos, pelo que entendi o primeiro registro deveria ser aceito.
Erro ao acessar http://localhost:8000/
DoesNotExist at /
Perfil matching query does not exist.
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.7.4
Exception Type: DoesNotExist
Exception Value:
Perfil matching query does not exist.
Exception Location: /Library/Python/2.7/site-packages/django/db/models/query.py in get, line 357
Python Executable: /usr/bin/python
Python Version: 2.7.10
Python Path:
['/Users/silvanioduartedacruz/connectedin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time: Thu, 30 Jun 2016 13:37:44 +0000
forms.py
from django import forms
from django.contrib.auth.models import User
class RegistrarUsuarioForm(forms.Form):
nome = forms.CharField(required=True)
email = forms.CharField(required=True)
senha = forms.CharField(required=True)
telefone = forms.CharField(required=True)
nome_empresa = forms.CharField(required=True)
def is_valid(self):
valid = True
if not super(RegistrarUsuarioForm, self).is_valid():
self.adiciona_erro('Por Favor, verifique os dados informados.')
valid = False
user_exists = User.objects.filter(username = self.data['nome']).exists()
if user_exists:
self.adiciona_erro('Usuario ja existe.')
valid = False
return valid
def adiciona_erro(self, message):
erros = self._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.utils.ErrorList())
erros.append(message)
view.py
from django.shortcuts import redirect
from django.shortcuts import render
from django.contrib.auth.models import User
from django.views.generic.base import View
from perfis.models import Perfil
from usuarios.forms import RegistrarUsuarioForm
# Create your views here.
class RegistrarUsuarioView(View):
template_name = 'registrar.html'
def get(self, request):
return render(request, self.template_name)
def post(self, request):
form = RegistrarUsuarioForm(request.POST)
if form.is_valid():
dados_form = form.data
usuario = User.objects.create_user(dados_form['nome'], dados_form['email'], dados_form['senha'])
perfil = Perfil(nome=dados_form['nome'],
email=dados_form['email'],
telefone=dados_form['telefone'],
nome_empresa=dados_form['nome_empresa'],
usuario=usuario)
perfil.save()
return redirect('index')
return render(request, self.template_name, {'form' : form})
registra.py
{% extends "base_usuario.html" %}
{% block body %}
<form class="form-signin" action="{% url 'registrar' %}" method="post" >
{% csrf_token %}
<h2 class="form-signin-heading">Crie seu Usuário</h2>
<input type="text" name="email" id="id_email"
class="form-control" placeholder="Email *" required>
<input type="text" name="nome" id="id_nome"
class="form-control" placeholder="Nome *" required>
<input type="text" name="senha" id="id_senha"
class="form-control" placeholder="Senha *" required>
<input type="text" name="telefone" id="id_telefone"
class="form-control" placeholder="Telefone" required>
<input type="text" name="empresa" id="id_empresa"
class="form-control" placeholder="Empresa" required>
<hr>
{% if form.errors %}
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ form.non_field_errors }}
</div>
{% endif %}
<button class="btn btn-lg btn-primary btn-block" type="submit" value="login"> Registrar</button>
</form>
{% endblock %}
models.py
from django.db import models
from django.contrib.auth.models import User
class Perfil(models.Model):
nome = models.CharField(max_length=255, null=False)
telefone = models.CharField(max_length=15, null=False)
nome_empresa = models.CharField(max_length=255, null=False)
contatos = models.ManyToManyField('self')
ususario = models.OneToOneField(User, related_name='perfil')
@property
def email(self):
return self.usuario.email
def convidar(self, perfil_convidado):
Convite(solicitante = self, convidado = perfil_convidado).save()
class Convite(models.Model): #implementa um relacionamento
solicitante = models.ForeignKey(Perfil, related_name='convites_feitos')
convidado = models.ForeignKey(Perfil, related_name='convites_recebidos')
def aceitar(self):
self.convidado.contatos.add(self.solicitante)
self.solicitante.contatos.add(self.convidado)
self.delete()