def get_queryset(self): ^ IndentationError: unindent does not match any outer indentation level

111 views
Skip to first unread message

Avitab Ayan Sarmah

unread,
May 18, 2018, 12:08:31 PM5/18/18
to Django users
While going through the django project i committed an error while executing "python manage.py runserver" inside my project directory.It is showing error is in line 24 of views.py.Please comment what exact the error is and how do i overcome this error.The exceptions and views.py is mentioned below:

Exceptions:

PS C:\Users\AVITABAYAN\mysite> python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000017BA5FB29D8>
Traceback (most recent call last):
  File "c:\python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "c:\python36\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "c:\python36\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "c:\python36\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "c:\python36\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "c:\python36\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "c:\python36\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "c:\python36\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:\python36\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "c:\python36\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "c:\python36\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\AVITABAYAN\mysite\mysite\urls.py", line 5, in <module>
    path('', include('polls.urls')),
  File "c:\python36\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "c:\python36\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Users\AVITABAYAN\mysite\polls\urls.py", line 3, in <module>
    from . import views
  File "C:\Users\AVITABAYAN\mysite\polls\views.py", line 24
    def get_queryset(self):
                          ^
IndentationError: unindent does not match any outer indentation level


views.py:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from django.utils import timezone

from . models import Choice, Question


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 settings
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):
        """Excludes any questions that aren't published yet.
        """
        return Question.objects.filter(pub_date__lte=timezone.now())


class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'


def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
#Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "you didn't select a choice.",
})
else:
selected_choice.votes +=1
selected_choice.save()
#...
#...
#...
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

James Farris

unread,
May 18, 2018, 12:39:35 PM5/18/18
to django...@googlegroups.com
The Indentation error is related to the spaces you have when you write your code. 

Based on looking at what you copy/pasted
The indentation of your def get_queryset is different from your other def’s. 

Python is very picky of how much or how little you indent your code for good reason. 

Everything that is nested inside something else should be 4 spaces or a tab in. For example a def should be 4 spaces in from its class, a variable should be 4 spaces in from its def. 

IndentationError: unindent does not match any outer indentation level

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/37bf86bc-9cc1-44b8-94e2-430a7daa557c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avitab Ayan Sarmah

unread,
May 18, 2018, 12:52:13 PM5/18/18
to Django users
Hi james,Please check my code clearly because i didn;t found any indentation error and everything is same

Avitab Ayan Sarmah

unread,
May 18, 2018, 1:06:05 PM5/18/18
to Django users
Thank you james i found the error and now another error i found please help me on this.You will see this as  another topic

Matthew Pava

unread,
May 18, 2018, 2:51:06 PM5/18/18
to django...@googlegroups.com

Avitab,

Whitespace is very important to Python.

You added an extra tab to your else and else clause of your try expression.

Consider the error message: unindent does not match outer indentation level

The message is mentioning indents, so let’s check our indents for any problems.

--

Reply all
Reply to author
Forward
0 new messages