所以我正在学习Django教程,在page的中间部分,我必须在 mysite \ polls \ views.py
中进行一些更改。这就是我按照class IndexView
和class 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<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'),
]
AND,这是我得到的错误:(请注意,我的缩进符合预期)
另外,下面的Traceback只有1个测试错误,有6个 SIMILAR TEST ERRORS WITH EXACT TRACEBACK
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
我尝试搜索此错误,但它要求的是正确缩进代码,它已经是。
答案 0 :(得分:6)
也许是因为你将标签与空格混合在一起。
答案 1 :(得分:2)
这种情况一直发生在我身上,特别是如果你做任何剪切和粘贴代码。有时你会在标签中找到一个空格,但它仍然“看起来”就像是一样。我建议你设置你的编辑器以使用空格作为标签(我使用四个空格作为标签),然后返回并删除所有的缩进,然后将它们添加回来,你将拥有一致的4个空格/制表符缩进,应该解决你的问题。
答案 2 :(得分:1)
字处理器通常用空格替换制表符,但事实并非如此。有时,如果您在同一文档中使用带空格的制表符,则解释程序不喜欢它。
通常,如果发生此错误,则可能会在上面的最后一个间距内以及标记的行中。我建议删除所有空格并将其重做为行。
如果出现相同的错误,则会出现制表符/空格问题,所以只需重做空白直到停止为止。
或者vim可以使用&lt;&lt;&lt;&lt;&lt;&lt;&lt;和&gt;&gt;在正常模式下。