简体   繁体   中英

Having trouble with Django forms “OperationalError: no such table” error

I'm trying to set up a form on Django that displays inputs on the page, but I get this error.

django.db.utils.OperationalError: no such table: firstapp_post

This doesn't happen right away, but when I try to use the submit feature on my form.

Right now this is what I have as my models:

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    post = models.CharField(max_length=500)
    user = models.ForeignKey(User)

These are currently my forms:

from django import forms
from firstapp.models import Post

class IndexForm(forms.ModelForm):
    post = forms.CharField()

    class Meta:
        model = Post
        fields = ('post',)

This is my views file:

from django.shortcuts import render, redirect
from firstapp.forms import IndexForm
from django.views.generic import TemplateView

class HomePage(TemplateView):
    template_name = 'home/home.html'

    def get(self, request):
        form = IndexForm()
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = IndexForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.user = request.user
            post.save()
            text = form.cleaned_data['post']
            form = IndexForm()
            return redirect('home:home')

        args = {'form': form, 'text': text}
        return render(request, self.template_name, args)

This is my base.html

{% load staticfiles %}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Assignment 4</title>
        <link rel='stylesheet' href='{% static "css/base.css" %}'/>
    </head>
    <body>
        <p>{{ variable }}</p>

        {% block body %}{% endblock %}

        <script src= '{% static "js/base.js" %}'></script>
    </body>
</html>

and my home.html:

{% extends 'base.html' %}

{% block body %}

<div class="container">
    <p>Home</p>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
    <p>{{ text }}</p>
</div>

{% endblock %}

Does anyone have any idea what this error even means or why I'm getting it? This has been driving me nuts. Thanks for the help!

As the error message mentions, that particular table does not exist in your database.

You can run the following command:

python manage.py makemigrations appname

By running makemigrations, you're telling Django that you've made some changes to your models and that you'd like the changes to be stored as a migration.

Now run migrate again to create those model tables in your database

python manage.py migrate

Further Reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM