【解決方法】コードのどこが間違っていますか?


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)    

私が試したこと:

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

解決策 1

見積もり:

IndentationError: unindent が外側のインデント レベルと一致しません

16 行目と、そのすぐ上と下の行を見てください。

Python
def detail(request, question_id):
   try:
     question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:

Python のインデントは重要です。左側にまったく同じ空白があるすべてのコードは、同じブロックの一部です。空白が変更されると、ブロックが終了し、新しいブロックが開始されます。
したがって、最初の行 try はそれ自体がブロックであり、システムは後続の行をどうするかをまったく理解できません。

コメント

タイトルとURLをコピーしました