Problems trying to save to "POST"="no-such-table" database Django / Python

2

I'm following a course in Python and Django, and the problem starts when the course version is 1.1.2 and mine is 1.8.2

I was able to generate a form to save "Events", but when I click submit, it returns the following error:

    OperationalError at /adiciona/
no such table: agenda_itemagenda
Request Method: POST
Request URL:    http://127.0.0.1:8000/adiciona/
Django Version: 1.8.2
Exception Type: OperationalError
Exception Value:    
no such table: agenda_itemagenda
Exception Location: C:\wamp\www\curso python\aula\aula\lib\site-packages\django-1.8.2-py2.7.egg\django\db\backends\sqlite3\base.py in execute, line 318
Python Executable:  C:\wamp\www\curso python\aula\aula\Scripts\python.exe
Python Version: 2.7.9
Python Path:    
['C:\wamp\www\curso python\aula\aula\gerenciador',
 'C:\wamp\www\curso python\aula\aula\lib\site-packages\django-1.8.2-py2.7.egg',
 'C:\WINDOWS\SYSTEM32\python27.zip',
 'C:\wamp\www\curso python\aula\aula\DLLs',
 'C:\wamp\www\curso python\aula\aula\lib',
 'C:\wamp\www\curso python\aula\aula\lib\plat-win',
 'C:\wamp\www\curso python\aula\aula\lib\lib-tk',
 'C:\wamp\www\curso python\aula\aula\Scripts',
 'C:\Python27\Lib',
 'C:\Python27\DLLs',
 'C:\Python27\Lib\lib-tk',
 'C:\wamp\www\curso python\aula\aula',
 'C:\wamp\www\curso python\aula\aula\lib\site-packages']
Server time:    Fri, 5 Jun 2015 01:10:35 -0300

agenda.py

from django.db import models

class ItemAgenda(models.Model):
    titulo = models.CharField(max_length=100)
    data = models.DateField()
    hora = models.TimeField()
    descricao = models.TextField()'

views.py

    #-*- encoding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
# Create your views here.


from models import ItemAgenda
from forms import FormItemAgenda

def lista(request):
    lista_itens = ItemAgenda.objects.all()
    return render_to_response("lista.html", {'lista_itens': lista_itens})


def adiciona(request):
    if request.method == "POST":
        form = FormItemAgenda(request.POST, request.FILES)
        if form.is_valid():
            dados = form.cleaned_data
            item = ItemAgenda(
                    titulo=dados['titulo'],
                    data=dados['data'],
                    hora=dados['hora'],
                    descricao=dados['descricao']
                    )
            item.save()
            return render_to_response("salvo.html", {})
    else:
        form = FormItemAgenda()
    return render_to_response("adiciona.html", {'form': form},
        context_instance=RequestContext(request))

forms.py

from django import forms

class FormItemAgenda(forms.Form):
    titulo = forms.CharField(max_length=100)
    data = forms.DateField(
                widget=forms.DateInput(format='%d/%m/%Y'),
                input_formats=['%d/%m/%Y', '%d/%m/%y'])
    hora = forms.TimeField()
    descricao = forms.CharField()
# Create your models here.

list.html

{% extends 'base.html' %}
{% block corpo %}
<a href="/adiciona/"> Adicionar novo item </a>
<ul>
    {% for item in lista_item %}
    <li> 
        <a href="/item/{{ item.id }}">
            {{ item.data|date:'d/m/Y'}} - {{ item.titulo }}

        </a>
    </li>
    {% empty %}
    <li> Sem itens na lista</li>
    {% endfor %}
</ul>
{% endblock %}

add.html

{% extends "base.html" %}

    {% block corpo %}

    <form action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Adicionar</button>

    </form>

    {% endblock %}
    
asked by anonymous 05.06.2015 / 07:01

2 answers

4

Most likely, you are trying to use a database that has not yet been updated. Did you use the commands?

python manage.py makemigrations
python manage.py migrate
python manage.py syncdb

These are for updating DB based on your model. link

I hope I have helped.

    
05.06.2015 / 15:09
3

As the exception says, no such table: agenda_itemagenda , the problem is that it is not possible to find the table that your application needs. Have you created the tables that the teacher is using in your course? Do you have a DDL (in other words, the CREATE TABLE commands to re-create the database) that are being used in your course? If so, you have to feed this file into SQLite in some way, either via the command line or through another Python script, e.g.

import sqlite3
conn = sqlite3.connect('example.db')  # arquivo com o banco de dados
c = conn.cursor()
with open('ddl.sql', 'r') as inputFile:  # arquivo com a DDL
    c.execute(inputFile.read())
conn.commit()
conn.close()
    
05.06.2015 / 13:25