【问题标题】:Querying works on shell but not on the Django program code查询适用于 shell,但不适用于 Django 程序代码
【发布时间】:2019-11-19 10:37:42
【问题描述】:

我是 python 和 Django 的新手。

我尝试了几种解决堆栈溢出问题的方法,但仍然遇到同样的问题。我试过从 shell 查询,它运行良好,但不是在我的代码上。

我已经使用

安装了pylint
pip install pylint-django

我还将 Settings > User Settings > Python 上的 Linter 设置pyLint 更改为 pylint_djangoflake8,但没有积极的结果. 我仍然收到消息“课堂课程没有'对象'成员 pylint(no-member)”

这些是我在 models.py 中的代码:

class Courses(models.Model):
course_title = models.CharField(max_length=200)
course_image = models.ImageField(upload_to='course_images/')
course_duration = models.TimeField() 

 def __str__(self):
 return self.course_title

views.py 看起来像这样:

from django.shortcuts import render
from django.http import HttpResponse
from .models import Courses

def homepage(request):
    cos = Courses.objects.all()
    context={ 'courses': cos }

    return render(request, "main/home.html", context) 

我需要帮助。我完全被困住了。

【问题讨论】:

    标签: django django-models django-views django-queryset django-cms


    【解决方案1】:

    这是来自 linter 的错误消息,而不是来自库。你不必担心这一点。由于 IDE 可能应用了默认的 linter 设置,这可能是造成这种情况的原因。此外,您可以为您的模型保留单数名称作为练习,如果您在管理员中注册该模型,它最后会自动给出 s

    应该是这样的

    from django.db impot models
    
    class Courses(models.Model):
        course_title = models.CharField(max_length=200)
        course_image = models.ImageField(upload_to='course_images/')
        course_duration = models.TimeField()
    
        def __str__(self):
            return self.course_title
    

    【讨论】:

      【解决方案2】:

      它一直就在我的眼皮底下。

      转到设置>搜索“终端”>打开任何"Edit in settings.json"并添加以下代码。

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

      注意: 确保在添加以下代码之前在最后一行的末尾添加一个逗号,否则会出现错误。

      保存,然后返回设置并搜索"lint" 检查搜索的左侧并找到Python Configuration

      确保“要使用的 Linter” 设置为 pyLintpylint_django

      应该没问题。

      【讨论】:

        猜你喜欢
        • 2020-08-23
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多