ROCA
ROCA

Reputation: 149

Creating Django´s blog Post custom fields

I am new to django, I am trying to add custom fields to the regular django blog Post App.

This is the blog fields out of the box:

models.py

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


STATUS = (
    (0,"Draft"),
    (1,"Publish")
)

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
    updated_on = models.DateTimeField(auto_now= True)
    content = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title

I need to create 4 fields:

I have tried simply adding those fields to my models.py below but it breaks the website:

title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
Types = models.CharField(max_length=200, unique=True)
etc

Error on my anaconda prompt: TabError: inconsistent use of tabs and spaces in indentation

Where else do I need to add the fields? DO I need to create a separate class? If so, how do I add to the create post backend?

Upvotes: 0

Views: 439

Answers (2)

Neville Lemein
Neville Lemein

Reputation: 183

The error means exactly as it says. Remember Python does not use curly brackets, it uses indentations. You have to make sure your lines are properly indented. I don't know the editor you are using but you can check whether there are formatting plugins or extensions for it. A quick solution though is to just delete all the white spaces then indent with the tab. Indentation is done via the tab key and not the space key.

Upvotes: 1

aminrd
aminrd

Reputation: 5000

When you indent in python, python just expects that indentation in first line should be the same as indentation in rest of the lines, be it 4 spaces, 1 space, 1 tab, or whatever.

As it is suggested in PEP-8, we should use "Spaces" rather than using "Tabs"! Anyway, the error says you probably added those new fields with inconsistent use of tabs and spaces in indentation (i.e. probably you are using tabs instead of 4 spaces)

Upvotes: 2

Related Questions