我有缩进错误。如何解决...?

时间:2019-04-03 13:02:39

标签: django python-3.x

运行服务器后,它为意外缩进给出了错误。 我该怎么办...... ????

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.DateTimeField(default=timezone.now)
    author= models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.title

2 个答案:

答案 0 :(得分:0)

如果有缩进问题,可以安装此库。这是一个例子。

pip install autopep8

sample.py

import os, sys

#Listing directories
os.system('ls -lah')

外壳:

autopep8 --in-place --aggressive --aggressive sample.py

输出:

import os
import sys

# Listing directories
os.system('ls -lah')

答案 1 :(得分:-1)

## This the Code of view.py ##



 from django.shortcuts import render
    from .models import Post

    posts=[

        {
            'author':'Sohaib',
            'title':'Blog Post 1',
            'content':'First post content',
            'date_posted':'28 March,2019',
        },
        {
            'author':'Shafiq',
            'title':'Blog Post 2',
            'content':'Second post content',
            'date_posted':'29 March,2019',
        }

    ]

    def home(request):
        context={
            'posts':Post.objects.all()
        }
        return render(request,'blog/home.html',context)

    def about(request):
        return render(request,'blog/about.html',{'title':'About'})