Click here to Skip to main content
15,868,080 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
from django.http import HttpResponse
from django.template import loader
from .models import Question
from django.shortcuts import render 
from django.http import Http404

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render (request, 'polls/index.html', context)
    

def detail(request, question_id):
   try:
     question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
    raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})

def results(reguest, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)
def vote(request, question_id):
    return HttpResponse("You're vooting on question %s." % question_id)    


What I have tried:

This error always appears, how can I fix it?

 File "C:\Users\user\Desktop\django\django_vs\mysite\polls\urls.py", line 2, in <module>
    from . import views
  File "C:\Users\user\Desktop\django\django_vs\mysite\polls\views.py", line 16
    except Question.DoesNotExist:
                                 ^
IndentationError: unindent does not match any outer indentation level
Posted
Updated 20-Nov-22 9:22am
Comments
Richard MacCutchan 21-Nov-22 4:28am    
Use consistent indentations of one tab, or four spaces, per indent. Apart from anything else it makes your code much easier to read.

1 solution

Quote:
IndentationError: unindent does not match any outer indentation level
Look at line 16, and the lines immediate above and below it:
Python
def detail(request, question_id):
   try:
     question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
Indentation in Python is critical: all code with exactly the same whitespace at the left is part of the same block: any change in whitespace ends the block and starts a new one.
So the first line in the try is a block on it's own, and the system cannot work out what to do with the subsequent lines at all.
 
Share this answer
 
Comments
Eva Smirnova 22-Nov-22 6:12am    
Thanck
OriginalGriff 22-Nov-22 6:22am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900