如何解决 Django 中的这些 Python 错误?

发布于 2024-12-28 09:29:53 字数 1566 浏览 1 评论 0原文

我正在尝试完成 Django 项目的教程。

我已经做到了这一点: https://docs.djangoproject.com/en/1.3/intro/tutorial02/#s-make-the-poll-app-modifying-in-the-admin 但我看到错误一些文件,例如在 admin.py 文件中:

from polls.models import Poll
from django.contrib import admin

admin.site.register(Poll)

我收到错误

Unresolved import: Poll

模型的定义如教程中所述:

import datetime

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice

该教程指出,当我在添加管理文件后重新启动服务器时,我应该看到投票应用程序在管理页面中引用在 127.0.0.1:8000,但我看到的是:

没有注册模型的模型管理员

我也看到

Unresolved import: Poll admin.py    /newProj/src/newProj/polls  line 0  PyDev Problem
Unresolved import: settings manage.py   /firstproject/firstproject  line 10 PyDev Problem
Unresolved import: settings manage.py   /newProj/src/newProj    line 10 PyDev 

这些错误是否是我看不到使用投票应用程序更新站点管理页面的原因?

我该如何解决这些错误?

I'm trying to make my way through the Django Project's tutorial.

I've made it as far as this: https://docs.djangoproject.com/en/1.3/intro/tutorial02/#s-make-the-poll-app-modifiable-in-the-admin but I'm seeing errors in some of the files, e.g. in the admin.py file:

from polls.models import Poll
from django.contrib import admin

admin.site.register(Poll)

I'm getting the error

Unresolved import: Poll

The models are defined as outlined in the tutorial:

import datetime

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice

The tutorial states that when I restart the server after adding the admin file, I should see the Poll app referenced in the Admin page at 127.0.0.1:8000, but all I'm seeing is this:

Model admin without registered models

I'm also seeing

Unresolved import: Poll admin.py    /newProj/src/newProj/polls  line 0  PyDev Problem
Unresolved import: settings manage.py   /firstproject/firstproject  line 10 PyDev Problem
Unresolved import: settings manage.py   /newProj/src/newProj    line 10 PyDev 

Are the errors the reason why I wouldn't be seeing the Site administration page updated with the Polls app?

How can I resolve these errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红尘作伴 2025-01-04 09:29:53

/newProj/src/newProj/polls/firstproject/firstproject... eeek,这看起来很诚实,就像一些问题的根源一样。好的,几个概念:

Python 包:所有 Python 应用程序都可以由包组成。这些基本上只是包含 __init__.py 文件的目录。

python如何查找包:嗯,python在查找包时可以做以下两件事之一:

  • 如果当前目录是一个包,即包含__init__.py,它可以从其中的文件导入。
  • 它可以搜索 python 路径(PYTHONPATH 环境变量,或 sys.path - 相同的东西),然后向下搜索子包。

django 项目的布局方式:默认的 django 项目看起来像这样:

project_folder/
    __init__.py
    settings.py      # config
    urls.py          # url config
    polls/
        __init__.py  # makes polls a package
        models.py    # models
        admin.py     # admin
        views.py     # app views
        forms.py     # per app forms
        urls.py      # per app urls

在这种布局中,当您 runserver< 时,您的 admin.py 有两种可能性/code>:

  • from models import Poll - 这有效,因为您仍在包的范围内。
  • from polls.models import Poll - 有效,因为 polls 是一个包,因此也是运行 runserver 的父包。

尽管这很诱人,但不要将 project_folder 用作包。一旦您重命名目录(例如,在部署期间),它就会破坏代码。

因此,考虑到所有这些:

  • 验证您的 __init__.py 文件存在于正确的位置,并且您使用的目录结构与上面的目录结构类似。可能最好也在 IDE 外部进行检查 - IDE 可能......很困难。
  • 验证您的路径正在搜索您认为在的位置。当前目录应该可以正常工作;如果不是,您可以看到

    导入系统
    打印系统路径
    

    将允许您查看您正在寻找的位置。您可以显式添加当前目录,但您不需要这样做。

  • 我不喜欢 PyDev。这里完全是个人喜好,但我发现它很难工作,超出了通常选择的编辑器+终端+文件浏览器。您也可能会这样,尤其是当您发现文件浏览器中的目录结构与您认为的 IDE 中的目录结构完全不同时。

/newProj/src/newProj/polls and /firstproject/firstproject... eeek, this looks to be honest like the source of a few problems. OK, a few concepts:

Python packages: All python applications can be comprised of packages. These are basically just directories containing a __init__.py file.

How python finds packages: Well, python can do one of two things when it looks for packages:

  • If the current directory is a package, i.e. contains __init__.py, it can import from files in it.
  • It can search on the python path (PYTHONPATH environment variable, or sys.path - same thing) and then down into sub packages.

How django projects are laid out: A default django project looks something like this:

project_folder/
    __init__.py
    settings.py      # config
    urls.py          # url config
    polls/
        __init__.py  # makes polls a package
        models.py    # models
        admin.py     # admin
        views.py     # app views
        forms.py     # per app forms
        urls.py      # per app urls

In this layout, there are two possibilities for your admin.py when you runserver:

  • from models import Poll - this works because you're remaining within the scope of your package.
  • from polls.models import Poll - works because polls is a package and so is the parent from where you're running runserver.

Although it's tempting, don't use project_folder as a package. It'll break the code as soon as you rename the dir (for example, during deployment).

So, with all that in mind:

  • verify your __init__.py files exist in the right places and that you're using a directory structure kinda like the one above. Might be best to check outside the IDE too - IDEs can be... difficult.
  • verify your path is searching where you think it is. The current directory should work just fine; if it isn't, you can see

    import sys
    print sys.path
    

    will allow you to view where you're looking. You can explicitly add the current directory, but you shouldn't need to.

  • I don't like PyDev. Totally personal preference here, but I find it hard work, over and above the usual editor of choice + terminal + file browser. You might too, especially if you find the directory structure is radically different in your file browser as compared to what you think you have with the IDE.
另类 2025-01-04 09:29:53

您正在使用 Pydev,它在导入过程中添加了额外的抽象层。

使用轻型文本编辑器和控制台进行学习。一旦 Python 路径和导入逻辑足够清晰,您就可以切换到 IDE,这样您就可以自己设置 Pydev。

You are using Pydev, which add an additional layer of abstraction on the importing process.

Use a light text editor and a console for learning. You can switch to an IDE once the Python Path and the import logic are clear enough in your head so you can setup Pydev yourself.

指尖上的星空 2025-01-04 09:29:53

这些错误是我看不到该网站的原因吗
使用民意调查应用程序更新管理页面?

这个问题的答案在同一页面,位于 https://docs.djangoproject.com/en/1.3/intro/tutorial02/#customize-the-admin-form

Are the errors the reason why I wouldn't be seeing the Site
administration page updated with the Polls app?

The answer to this question is in the same page, at https://docs.djangoproject.com/en/1.3/intro/tutorial02/#customize-the-admin-form

离去的眼神 2025-01-04 09:29:53

试试这个:

import newProj.poll.models

try this:

import newProj.poll.models
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文