The data is not being saved in the Database and Django is not generating Error,
but when I create an object of Livro
and try to save by the shell it returns me the following error:
ValueError: invalid literal for int () with base 10:
I tried to search for solutions but to no avail! I'm using sqlite3! Django 1.6.2 and python 2.7! The project is available in github .
Models.py:
class Biblioteca(models.Model):
endereco = models.CharField(max_length = 80)
nome = models.CharField(max_length = 60)
telefone = models.IntegerField(max_length = 11)
def __unicode__(self):
return self.nome
class Livro(models.Model):
codigo = models.CharField(max_length = 10)
publicacao = models.IntegerField()
autor = models.CharField(max_length = 80)
editora = models.CharField(max_length = 45)
genero = models.CharField(max_length = 45)
sinopse = models.CharField(max_length=150)
titulo = models.CharField(max_length = 150)
biblioteca = models.ForeignKey(Biblioteca)
def __unicode__(self):
return (self.titulo, self.autor)
Forms.py:
class FormLivro(forms.Form):
codigo = forms.CharField(max_length=10)
publicacao = forms.IntegerField()
autor = forms.CharField(max_length=80)
editora = forms.CharField(max_length=45)
genero = forms.CharField(max_length=45)
sinopse = forms.CharField(max_length=150)
titulo = forms.CharField(max_length=150)
def save(self):
codigo = self.cleaned_data.get('codigo')# Acessando os Fields
publicacao = self.cleaned_data.get('publicacao')
autor = self.cleaned_data.get('autor')
editora = self.cleaned_data.get('editora')
genero = self.cleaned_data.get('genero')
sinopse = self.cleaned_data.get('sinopse')
titulo = self.cleaned_data.get('titulo')
novo_livro = Livro(
codigo = codigo,
publicacao = publicacao,
autor = autor,
editora = editora,
genero = genero,
sinopse = sinopse,
titulo = titulo
)
novo_livro.save()
return novo_livro
def clean_codigo(self):
codigo = self.cleaned_data.get('codigo')
if Livro.object.filter(email = email):
raise forms.ValidationError('Codigo já cadastrado!')
return codigo
Views.py:
def cadLivro(request):
livros = Livro.objects.all() # Lista de livros
if request.method == "POST":
form = FormLivro(request.POST)
if form.is_valid(): # Processando o Formulario
novo_livro = form.save()
HttpResponseRedirect(reverse('nCadLivro'))
else:
form = FormLivro()
return render(request, 'cadastro_livro.html')