Friday, May 18, 2018

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

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.

 

 

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,)))

 

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Avitab Ayan Sarmah
Sent: Friday, May 18, 2018 11:09 AM
To: Django users
Subject: def get_queryset(self): ^ IndentationError: unindent does not match any outer indentation level

 

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,)))

 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

No comments:

Post a Comment