[Python] OperationalError, No such table: accounts_user

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    My Django backend for a to-do list website authenticates users by asking their email, sending a link with a token using that to log them in, redirecting them back to the homepage. When I run the development server I get:


    OperationalError at / no such table: accounts_user.
    ​

    I have two apps that are part of the project, "lists" and "accounts". Both are listed in INSTALLED_APPS under core.settings. I tried deleting my SQLite database, all __pycache__ and migrations folders and running makemigrations and migrate again. Even for the accounts app using --run-syncdb and that still didn't work.

    The error is thrown during template rendering on line 17 of base.py, the one that says {% if user.email %}. I'm using Django 1.11.29 and Python 3.6.8. The database is an SQLite file in the project directory.

    lists/models.py:

    from django.db import models
    from django.core.urlresolvers import reverse

    class List(models.Model):
    def get_absolute_url(self):
    return reverse('view_list', args=[self.id])

    class Item(models.Model):
    class Meta:
    ordering = ('id',)
    unique_together = ('list', 'text',)

    def __str__(self):
    return self.text

    text = models.TextField(default='')
    list = models.ForeignKey(List, default=None)


    lists/views.py:

    from django.shortcuts import redirect, render
    from django.core.exceptions import ValidationError
    from django.utils.html import escape

    from lists.forms import ExistingListItemForm, ItemForm
    from lists.models import Item, List

    def home_page(request):
    return render(request, 'home.html', {'form': ItemForm()})

    def new_list(request):
    form = ItemForm(data=request.POST)

    if form.is_valid():
    list_ = List.objects.create()
    form = ItemForm(data=request.POST)
    form.add_list(list_)
    form.save()
    return redirect(list_)
    else:
    return render(request, 'home.html', {"form": form})

    def view_list(request, list_id):
    list_ = List.objects.get(id=list_id)
    form = ExistingListItemForm(for_list=list_)

    if request.method == 'POST':
    form = ExistingListItemForm(for_list=list_, data=request.POST)
    if form.is_valid():
    form.save()
    return redirect(list_)

    return render(request, 'list.html', {'list': list_, "form": form})


    accounts/models.py:

    import uuid

    from django.contrib import auth
    from django.db import models
    from django.db.utils import OperationalError

    try:
    auth.signals.user_logged_in.disconnect(auth.models.update_last_login)
    except OperationalError:
    pass

    class User(models.Model):
    email = models.EmailField(primary_key=True)

    REQUIRED_FIELDS = []
    USERNAME_FIELD = 'email'
    is_anonymous = False
    is_authenticated = True

    class Token(models.Model):
    email = models.EmailField()
    uid = models.CharField(default=uuid.uuid4, max_length=40)


    accounts/views.py:

    import uuid

    from django.contrib import auth
    from django.db import models
    from django.db.utils import OperationalError

    try:
    auth.signals.user_logged_in.disconnect(auth.models.update_last_login)
    except OperationalError:
    pass

    class User(models.Model):
    email = models.EmailField(primary_key=True)

    REQUIRED_FIELDS = []
    USERNAME_FIELD = 'email'
    is_anonymous = False
    is_authenticated = True

    class Token(models.Model):
    email = models.EmailField()
    uid = models.CharField(default=uuid.uuid4, max_length=40)


    lists/templates/base.py:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>To-Do lists</title>
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/base.css" rel="stylesheet">
    </head>
    <body>
    <div class"container">

    <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <a class="navbar-brand" href="/">Superlists</a>
    {% if user.email %}
    <ul class="nav navbar-nav navbar-right">
    <li class="navbar-text">Logged in as {{ user.email }}</li>
    <li><a href="#">Log out</a></li>
    </ul>
    {% else %}
    <form class="navbar-form navbar-right"
    method="POST"
    action="{% url 'send_login_email' %}">
    <span>Enter email to log in:</span>
    <input class="form-control" name="email" type="text" />
    {% csrf_token %}
    </form>
    {% endif %}
    </div>
    </nav>

    {% if messages %}
    <div class="row">
    <div class="col-md-8">
    {% for message in messages %}
    {% if message.level_tag == 'success' %}
    <div class="alert alert-success">{{ message }}</div>
    {% else %}
    <div class="alert alert-warning">{{ message }}</div>
    {% endif %}
    {% endfor %}
    </div>
    </div>
    {% endif %}

    <div class="row">
    <div class="col-md-6 col-md-offset-3 jumbotron">
    <div class="text-center">
    <h1>{% block header_text %}{% endblock %}</h1>
    <form method="POST" action="{% block form_action %}{% endblock %}">
    {{ form.text }}
    {% csrf_token %}
    {% if form.errors %}
    <div class="form-group has-errors">
    <span class="help-block">{{ form.text.errors }}</span>
    </div>
    {% endif %}
    </form>
    </div>
    </div>
    </div>

    <div class="row">
    <div class="col-md-6 col-md-offset-3">
    {% block table %}
    {% endblock %}
    </div>
    </div>

    </div>
    <script src="/static/jquery-3.1.1.js"></script>
    <script src="/static/list.js"></script>

    <script>

    $(document).ready(function() {
    window.Superlists.initialize();
    });

    </script>

    </body>
    </html>

    Continue reading...

Compartilhe esta Página