当前位置:  开发笔记 > 编程语言 > 正文

python中的"缩进错误:意外缩进"

如何解决《python中的"缩进错误:意外缩进"》经验,为你挑选了1个好方法。

所以我正在学习Django教程,在页面的中间部分,我必须在mysite\polls\views.py中进行一些更改.

这就是我如何进行更改class IndexViewclass DetailView按照要求:

mysite的\调查\ 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())

mysite的\调查\ urls.py:

from django.conf.urls import url

from . import views

app_name = 'polls'
urlpatterns = [
    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'),
    ]

AND,这是我得到的错误:( 请注意,我的缩进是正确的,如预期的那样)

另外,下面的Traceback只有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 
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 
from . import views
File "E:\ict\python\mysite\polls\views.py", line 23
def get_queryset(self): #From The DetailView Class
^
IndentationError: unexpected indent

我尝试搜索这个错误,但它要求的是正确缩进代码,它已经是.



1> Bagus Aji Sa..:

也许是因为你将标签与空格混合在一起.

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有