如何解决 Django 中的这些 Python 错误?
我正在尝试完成 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
/newProj/src/newProj/polls
和/firstproject/firstproject
... eeek,这看起来很诚实,就像一些问题的根源一样。好的,几个概念:Python 包:所有 Python 应用程序都可以由包组成。这些基本上只是包含
__init__.py
文件的目录。python如何查找包:嗯,python在查找包时可以做以下两件事之一:
__init__.py
,它可以从其中的文件导入。PYTHONPATH
环境变量,或sys.path
- 相同的东西),然后向下搜索子包。django 项目的布局方式:默认的 django 项目看起来像这样:
在这种布局中,当您
runserver< 时,您的
admin.py
有两种可能性/code>:from models import Poll
- 这有效,因为您仍在包的范围内。from polls.models import Poll
- 有效,因为polls
是一个包,因此也是运行runserver
的父包。尽管这很诱人,但不要将
project_folder
用作包。一旦您重命名目录(例如,在部署期间),它就会破坏代码。因此,考虑到所有这些:
验证您的路径正在搜索您认为在的位置。当前目录应该可以正常工作;如果不是,您可以看到
将允许您查看您正在寻找的位置。您可以显式添加当前目录,但您不需要这样做。
/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:
__init__.py
, it can import from files in it.PYTHONPATH
environment variable, orsys.path
- same thing) and then down into sub packages.How django projects are laid out: A default django project looks something like this:
In this layout, there are two possibilities for your
admin.py
when yourunserver
:from models import Poll
- this works because you're remaining within the scope of your package.from polls.models import Poll
- works becausepolls
is a package and so is the parent from where you're runningrunserver
.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:
__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
will allow you to view where you're looking. You can explicitly add the current directory, but you shouldn't need to.
您正在使用 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.
这个问题的答案在同一页面,位于 https://docs.djangoproject.com/en/1.3/intro/tutorial02/#customize-the-admin-form
The answer to this question is in the same page, at https://docs.djangoproject.com/en/1.3/intro/tutorial02/#customize-the-admin-form
试试这个:
try this: