首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能使用Visual代码为这个Django项目启用Pylint?

问为什么我不能使用Visual代码为这个Django项目启用Pylint?
EN

Stack Overflow用户
提问于 2020-12-22 12:06:44
回答 1查看 588关注 0票数 1

我开始使用Django Python框架(按照著名的Mozilla示例项目开发一个Ubuntu 20.04 )。这个:页面

我使用的是Python3.8.5版本。

我发现了以下问题(在堆栈溢出上,我看到很多人都有这个问题,这应该与Pylint有关)。

基本上,这里暴露的问题是相同的:https://stackoverflow.com/questions/49951637/django-visual-studio-tutorial-objects-method-error

我有这个Django views.py文件:

代码语言:javascript
复制
from django.shortcuts import render

# Create your views here.

from .models import Book, Author, BookInstance, Genre   

def index(request):
    """View function for home page of site."""

    # Generate counts of some of the main objects
    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()

    # Available books (status = 'a')
    num_instances_available = BookInstance.objects.filter(status__exact='a').count()

    # The 'all()' is implied by default.
    num_authors = Author.objects.count()

    context = {
        'num_books': num_books,
        'num_instances': num_instances,
        'num_instances_available': num_instances_available,
        'num_authors': num_authors,
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, 'index.html', context=context)

问题是Visual代码给出了以下在线错误,如下所示:

代码语言:javascript
复制
num_books = Book.objects.all().count()

错误是:

类“Book”没有“对象”成员(非成员)

这是我的图书类代码:

代码语言:javascript
复制
class Book(models.Model):
    """Model representing a book (but not a specific copy of a book)."""
    title = models.CharField(max_length=200)
    author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
    # Foreign Key used because book can only have one author, but authors can have multiple books
    # Author as a string rather than object because it hasn't been declared yet in file.
    summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
    isbn = models.CharField('ISBN', max_length=13,
                            unique=True,
                            help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn'
                                      '">ISBN number</a>')
    genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
    # ManyToManyField used because a genre can contain many books and a Book can cover many genres.
    # Genre class has already been defined so we can specify the object above.
    language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
    
    class Meta:
        ordering = ['title', 'author']

    def display_genre(self):
        """Creates a string for the Genre. This is required to display genre in Admin."""
        return ', '.join([genre.name for genre in self.genre.all()[:3]])

    display_genre.short_description = 'Genre'

    def get_absolute_url(self):
        """Returns the url to access a particular book instance."""
        return reverse('book-detail', args=[str(self.id)])

    def __str__(self):
        """String for representing the Model object."""
        return self.title

我的代码工作正常,因此错误与我的代码无关,但它是一个lint错误。

我试着遵循这个链接中指出的步骤:https://stackoverflow.com/questions/49951637/django-visual-studio-tutorial-objects-method-error

但这是行不通的。我的IDE中仍然有这个错误。

我是这样做的:

  1. 我安装了这两个软件包: pip3安装pylint pip3安装

我在全局环境和我的项目使用的虚拟环境中安装了这些包。

  1. 然后我进入了这个文件夹:~/Documenti/Python-WS/django_projects/locallibrary/.vscode$ (localibrary)是与我的项目相关的文件夹,我编辑了settings.json文件,这个文件: (本地库-教程-ENV) andrea@ubuntu:~/Documenti/Python-WS/django_projects/locallibrary/.vscode$ ls -a 。。。settings.json

我用这样的方式编辑它:

代码语言:javascript
复制
{
    "python.pythonPath": "/home/andrea/Documenti/Python-WS/Environments/LOCAL-L>
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "[python]": {

    },
    "python.linting.pylintArgs": [
        "--load-plugins", "pylint_django"
    ],
}

然后我重新启动了Visual代码,但它在IDE中仍然存在以下错误:

​

​

我的设置有什么问题?如何在我的项目中启用Python和Django lint?我遗漏了什么?

此外,是否有一种为所有Python项目全局启用这个lint功能的方法,以避免为每个项目设置这个lint功能?

EN

回答 1

Stack Overflow用户

发布于 2020-12-22 17:59:43

我自己解决了这个问题,方法如下:

  1. 菜单文件settings.json Preferences→Settings→在我点击“编辑in→”的搜索结果中搜索“终端”→(过程在https://www.youtube.com/watch?v=eMIxokGhFHM中显示)
  2. 与前面的链接不同,我在与Visual相关的settings.json文件中添加了更多与Django相关的Pylint设置: { "window.zoomLevel":1、"typescript.updateImportsOnFileMove.enabled":“始终”、"terminal.integrated.automationShell.linux":“terminal.integrated.automationShell.linux”、"python.linting.flake8Enabled":true、"python.linting.pydocstyleEnabled":true、"python.linting.pydocstyleArgs":

现在它很好用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65408445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档