-1

I'm trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out "inconsistent use of tabs and spaces in indentation" when I try to run the program.

I'm a beginner in python programming. I would be glad if I could get some overall tips on my code.

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


    class Post(models.Model):
        title = models.CharField(max_length=100)
        content = models.TextField()
        date_posted = models.DatTimeField(defult=timezone.now)
        author = models.ForeignKey(User, on_delete=models.CASCADE)
  • Most editors that provide automatic tab replacement will have an option to enable/disable that feature. Look around in your options or preferences menu. – Kevin Feb 04 '19 at 15:49
  • Convert all tabs `\t` to 4 spaces. Python doesnt allow to mix both. – Maurice Meyer Feb 04 '19 at 15:49
  • And be sure your editor does not extend tabs to spaces. Some will simply insert 4 spaces when you press "TAB". – gonczor Feb 04 '19 at 15:50

1 Answers1

1

That simply means that you're using a mix of spaces and tabs to indent your code, and/or that the number of spaces/tabs is inconsistent (say, sometimes you use 2, others 4 spaces). Remember, in Python indentation is very, very important!. For example, your code should look like this to be correct:

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

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DatTimeField(defult=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

Use a good IDE or text editor to avoid this kind of mistakes, and make sure that your code is, well, "pretty" before executing it. Using proper indentation in Python is not optional, and that's great! it forces you to write code that is easy to read for others.

Óscar López
  • 232,561
  • 37
  • 312
  • 386