[SOLVED] Django “ProgrammingError at /create-post” usnig heroku postgres as database host

Issue

This Content is from Stack Overflow. Question asked by Franciso Reyna

I am trying to create a blog website in django using postgres with heroku in the cloud. When trying to submit the post using forms from django I get this error “ProgrammingError at /create-post
column “title” of relation “main_post” does not exist
LINE 1: INSERT INTO “main_post” (“author_id”, “title”, “description”… “

This is my view for creating posts

from django.shortcuts import render, redirect
from .forms import RegisterForm, PostForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, logout, authenticate



@login_required(login_url="/login")
def home(request):
    return render(request, 'main/home.html')

@login_required(login_url="/login")
def create_post(request):
    if request.method =='POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect("/home")
    else:
        form = PostForm()
    return render(request, 'main/create_post.html', {"form": form})

This is my forms.py file

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import Post

class RegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]


class PostForm(forms.ModelForm):
    
    class Meta:
        model = Post
        fields = ["title", "description"]

This is my Post model

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


class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title + "n" + self.description

This is the error

enter image description here



Solution

Delete your migrations folder and re-migrate like this:

python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001
python manage.py migrate

And then try


This Question was asked in StackOverflow by Franciso Reyna and Answered by Manoj Tolagekar It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?