Python Forum
IndentationError: unexpected indent in views.py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndentationError: unexpected indent in views.py
#1
Sir,
I am trying to create project in Django using GET and POST request mechanism
but Views.py file giving some error in line 25 so please help me out to resolve this error ...

from django.http import HttpResponse
from django.template import loader
from django.template import engines
from django.template.loader import render_to_string
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    if request.method=='POST':
        name = request.POST.get('name')
        v1=request.POST.get('v1')
        v2=request.POST.get('v2')
        v3=int(v1)+int(v2)
about_template = '''
   <html>
       <head>
    <title>Home Page</title>
       </head>
   <body>
    <center>
    Name '''+str(name)+''' and the sum is '''+str(v3)+'''
    <center><p><h1>Thank You</h1></center>
   </body>
   </html>'''
    django_engine = engines['django']
    template=django_engine.from_string(about_template)
    html=template.render()
    return HttpResponse(html)
 else:
    template=loader.get_template('index.html')
    return HttpResponse(template.render())
Reply
#2
from django.http import HttpResponse
from django.template import loader
from django.template import engines
from django.template.loader import render_to_string
from django.views.decorators.csrf import csrf_exempt
 
@csrf_exempt
def index(request):
    if request.method=='POST':  ##<---you are inside the "index" function here
        name = request.POST.get('name')
        v1=request.POST.get('v1')
        v2=request.POST.get('v2')
        v3=int(v1)+int(v2)
about_template = '''             ###<--- here you are back at the global level
   <html>
       <head>
    <title>Home Page</title>
       </head>
   <body>
    <center>
    Name '''+str(name)+''' and the sum is '''+str(v3)+'''
    <center><p><h1>Thank You</h1></center>
   </body>
   </html>'''
    django_engine = engines['django']
    template=django_engine.from_string(about_template)
    html=template.render()
    return HttpResponse(html)
 else:                                                  ## <-- here is your "else" that is at the 
                                                        ## global   level
    template=loader.get_template('index.html')          ## but it needs to be on the same level as 
    return HttpResponse(template.render())              ## as your "if"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django views : positional argument ghoul 0 1,441 Nov-15-2021, 06:02 PM
Last Post: ghoul
  changing stylesheet according to variable in views AhmdMhmdWddh 0 1,571 Feb-21-2021, 12:57 PM
Last Post: AhmdMhmdWddh
  Posting html values to views/models in Django Malt 1 2,155 Sep-04-2019, 01:44 PM
Last Post: fishhook
  IndentationError: unexpected indent salahhadjar 2 4,392 Nov-04-2018, 06:10 PM
Last Post: salahhadjar
  Unexpected indent / invalid syntax tjnichols 38 18,978 May-16-2018, 10:24 PM
Last Post: tjnichols

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020