&引用;缩进错误:意外缩进“;用python
因此,我正在学习Django教程,在教程的中间部分,我必须在mysite\polls\views.py 这就是我如何按照要求在&引用;缩进错误:意外缩进“;用python,python,django,Python,Django,因此,我正在学习Django教程,在教程的中间部分,我必须在mysite\polls\views.py 这就是我如何按照要求在class IndexView和class DetailView中进行更改的原因: mysite\polls\views.py: class IndexView(generic.ListView): template_name = 'polls/index.html' context_object_name = 'latest_question_list'
class IndexView
和class DetailView
中进行更改的原因:
mysite\polls\views.py:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
return Question.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
def get_queryset(self): #The Error Points Here
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now())
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
mysite\polls\url.py:
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""
Return the last five published questions (not including those set to be
published in the future).
"""
return Question.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
def get_queryset(self): #The Error Points Here
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now())
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
从django.conf.url导入url
从…起导入视图
应用程序名称='polls'
URL模式=[
url(r'^$',views.IndexView.as_view(),name='index'),
url(r'^(?P[0-9]+)/$',views.DetailView.as_view(),name='detail'),
url(r'^(?P[0-9]+)/results/$',views.ResultsView.as_view(),name='results'),
url(r'^(?P[0-9]+)/vote/$',views.vote,name='vote'),
]
这就是我得到的错误:(请注意,我的缩进与预期一样正确)
此外,下面的回溯仅包含1个测试错误,有6个类似的测试错误具有精确的回溯
PS E:\ict\python\mysite> python manage.py test polls
Creating test database for alias 'default'...
EE.EEEEE
======================================================================
ERROR: test_detail_view_with_a_future_question (polls.tests.QuestionIndexDetailTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "E:\ict\python\mysite\polls\tests.py", line 113, in test_detail_view_with_a_future_question
args=(future_question.id,)))
File "C:\Program Files\Python27\lib\site-packages\django\core\urlresolvers.py", line 568, in reverse
app_list = resolver.app_dict[ns]
File "C:\Program Files\Python27\lib\site-packages\django\core\urlresolvers.py", line 360, in app_dict
self._populate()
File "C:\Program Files\Python27\lib\site-packages\django\core\urlresolvers.py", line 293, in _populate
for pattern in reversed(self.url_patterns):
File "C:\Program Files\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Program Files\Python27\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Program Files\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Program Files\Python27\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Program Files\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "E:\ict\python\mysite\mysite\urls.py", line 21, in <module>
url(r'^polls/', include('polls.urls')),
File "C:\Program Files\Python27\lib\site-packages\django\conf\urls\__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "C:\Program Files\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "E:\ict\python\mysite\polls\urls.py", line 3, in <module>
from . import views
File "E:\ict\python\mysite\polls\views.py", line 23
def get_queryset(self): #From The DetailView Class
^
IndentationError: unexpected indent
PS E:\ict\python\mysite>python manage.py测试轮询
正在为别名“default”创建测试数据库。。。
嗯,嗯
======================================================================
错误:测试\详细信息\查看\带有\未来\问题(polls.tests.QuestionIndexDetailTests)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“E:\ict\python\mysite\polls\tests.py”,第113行,测试详细信息视图,带未来问题
args=(future_question.id,))
文件“C:\Program Files\Python27\lib\site packages\django\core\urlresolvers.py”,第568行,相反
应用程序列表=解析器。应用程序目录[ns]
文件“C:\Program Files\Python27\lib\site packages\django\core\urlresolvers.py”,第360行,在应用程序目录中
自我.()
文件“C:\Program Files\Python27\lib\site packages\django\core\urlresolvers.py”,第293行,在
对于反向模式(self.url\u模式):
文件“C:\Program Files\Python27\lib\site packages\django\utils\functional.py”,第33行,在__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
文件“C:\Program Files\Python27\lib\site packages\django\core\urlresolvers.py”,第417行,url\U模式
patterns=getattr(self.urlconf_模块,“urlpatterns”,self.urlconf_模块)
文件“C:\Program Files\Python27\lib\site packages\django\utils\functional.py”,第33行,在__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
urlconf_模块中的文件“C:\Program Files\Python27\lib\site packages\django\core\urlresolvers.py”,第410行
返回导入_模块(self.urlconf_名称)
文件“C:\Program Files\Python27\lib\importlib\\uuuuu init\uuuuuu.py”,第37行,在导入模块中
__导入(名称)
文件“E:\ict\python\mysite\mysite\url.py”,第21行,在
url(r“^polls/”,包括('polls.url'),
文件“C:\Program Files\Python27\lib\site packages\django\conf\urls\\ uuuu init\uuuu.py”,第52行,包含在include中
urlconf_模块=导入_模块(urlconf_模块)
文件“C:\Program Files\Python27\lib\importlib\\uuuuu init\uuuuuu.py”,第37行,在导入模块中
__导入(名称)
文件“E:\ict\python\mysite\polls\url.py”,第3行,在
从…起导入视图
文件“E:\ict\python\mysite\polls\views.py”,第23行
def get_queryset(self):#来自DetailView类
^
缩进错误:意外缩进
我试图搜索这个错误,但它要求的只是正确地缩进代码,而代码已经缩进了。可能是因为您将制表符与空格混合在一起了 文字处理器通常用空格代替制表符,但事实并非如此。有时,如果在同一文档上使用带空格的制表符,解释器会不喜欢它 通常,如果发生此错误,它很可能位于上面最后一个类似的间隔内,以及标记的线。我建议删除所有的空白,并将其重新添加到行中 如果相同的错误进一步出现,这是一个制表符/空格问题,所以只需继续重复空白,直到它停止
或者,vim可以在正常模式下使用>进行压痕 对我来说,这种情况经常发生,尤其是如果你剪切和粘贴代码的话。有时候你会在标签上留下一个空格,但看起来还是一样的。我建议您将编辑器设置为使用空格作为制表符(我使用四个空格作为制表符),然后返回并删除所有缩进,然后再添加它们,您将获得一致的4个空格/制表符缩进,这将解决您的问题 你能用两种不同的方式命名你的函数get_queryset吗,这样stacktrace就更清晰了?如果我们只看到一部分,第23行就没什么意义了code@iScrE4mget_queryset错误点来自DetailView类。我编辑了这个问题来消除困惑。哦,天哪。成功了!记事本++中作用域的不寻常外观也消失了!我不知道我可以在编辑器中为选项卡设置空格。我会做第一件事!谢谢。:)@机器人先生,如果你在windows上,我建议使用Sublimate2作为编辑。。。升华2太棒了。Atom.io也非常相似。两者都是跨平台的,非常棒。如果使用git,还可以通过插件将git集成到这两个编辑器中。