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

Asked

Viewed 738 times

2

I’m following a course by Python and Django, and the problem begins when the version in the course is 1.1.2 and mine is 1.8.2

I managed to generate a form to save "Events" however, when clicking on Ubmit, 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()`

py views.

    #-*- 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.

html list.

{% 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 %}

adds.html

{% extends "base.html" %}

    {% block corpo %}

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

    </form>

    {% endblock %}

2 answers

4

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

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

These are to update the BD based on your model. https://docs.djangoproject.com/en/1.8/intro/tutorial01/

I hope I’ve helped.

  • So, the problem was the compatibility between versions even... I managed to continue without problems installing the correct version ! thank you for your attention !

3

As the exception says, no such table: agenda_itemagenda, the problem is that it is not possible to find the table that your app needs. Have you created the tables that the teacher is using in your course? You has the DDL (in other words, the commands CREATE TABLE to recreate the database) being used in your course? If so, you have to feed this file to Sqlite in some way, either via command line or via another Python script, and.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()
  • the problem was version incompatibility, I regressed my Django to 1.2.1 and managed to proceed with the course. I thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.