0

I am learning fresh django development. Working on a blog project I am facing a issue that, while publishing a new blog, that doesn't redirects to home/blogs page. I tried to manually to go home It says,

OperationalError at /blog/ no such column: App_Blog_blog.author_id

I tried to debug several times, searched in documentation can't get away with this author_id. These are my codes:

Views.py

class CreateBlog(LoginRequiredMixin, CreateView):
    model = Blog
    template_name = 'App_Blog/create_blog.html'
    fields = ('blog_title', 'blog_content', 'blog_image',)

    def form_valid(self, form):
        blog_obj = form.save(commit=False)
        blog_obj.author = self.request.user
        title = blog_obj.blog_title
        blog_obj.slug = title.replace(" ", "-") + "-" + str(uuid.uuid4())
        blog_obj.save()
        return HttpResponseRedirect(reverse('index'))


class BlogList(ListView):
    context_object_name = 'blogs'
    model = Blog
    template_name = 'App_Blog/blog_list.html'

Views.py of main project file

def index(request):
    return HttpResponseRedirect(reverse('App_Blog:blog_list'))

urls.py

from django.urls import path
from App_Blog import views

app_name = 'App_Blog'

urlpatterns = [
    path('', views.BlogList.as_view(), name='blog_list'),
    path('write/', views.CreateBlog.as_view(), name='create_blog'),
]

models.py

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

# Create your models here.


class Blog(models.Model):
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='post_author')
    blog_title = models.CharField(max_length=264, verbose_name="Put a Title")
    slug = models.SlugField(max_length=264, unique=True)
    blog_content = models.TextField(verbose_name="What is on your mind?")
    blog_image = models.ImageField(
        upload_to='blog_images', verbose_name="Image")
    publish_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.blog_title

create_blog.html

{% extends 'base.html' %}
{% load crispy_forms_tags %}

{% block title_block %}
Write a Blog
{% endblock title_block %}


{% block body_block %}
<h2>Start Writing:</h2>
<form method="POST">
    {{ form | crispy }}
    {% csrf_token %}
    <br>
    <button type="button" class="btn btn-success btn-sm">Publish</button>
</form>
{% endblock body_block %}

blog_list.html

{% extends 'base.html' %}
{% block title_block %} Home {% endblock %}


{% block body_block %}

{% for blog in blogs %}
<h3>{{blog.blog_title}}</h3>
<h6>{{blog.publish_date}}</h6>
{% endfor %}

{% endblock %}

Create Blog form:

enter image description here

Error showing on site:

enter image description here

  • Can you also add the models code? – Tamir May 02 '21 at 12:03
  • @Tamir Sorry missed that. updated now – Mohammad Miskatur Rahman May 02 '21 at 12:05
  • Did you make migrations and migrated the database? – Willem Van Onsem May 02 '21 at 12:05
  • Have you migrated your models? I think that's the issue – Tamir May 02 '21 at 12:05
  • @WillemVanOnsem no didn't. I just tried that and its saying, "You are trying to add a non-nullable field 'author' to blog without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py – Mohammad Miskatur Rahman May 02 '21 at 12:09
  • @Tamir "You are trying to add a non-nullable field 'author' to blog without a default; we can't do that (the database needs something to populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py – Mohammad Miskatur Rahman May 02 '21 at 12:10
  • Are you populating an existing DB? Or are you working with a new one? Basically you can find an answer here: [answer](https://stackoverflow.com/questions/26185687/you-are-trying-to-add-a-non-nullable-field-new-field-to-userprofile-without-a) – Tamir May 02 '21 at 12:12
  • @Tamir I am working on a new project. I added default='id' and migrated then this shows up, "ValueError: Field 'id' expected a number but got 'author'." – Mohammad Miskatur Rahman May 02 '21 at 12:51
  • The best thing you can refer to is Django's documentation and understand how migrations and models work (This way you will actually learn what the issue is and not only a patch fix) – Tamir May 02 '21 at 12:53

0 Answers0