0

I'm feeling a rabbit posting this question, but I don't continue because this: What's is wrong with this stretch of my code:

When I put something after the "if" I receive:

IndentationError: unindent does not match any outer indentation level

For example, if I put "cont =3" after the "if" and before "post = form2.save(commit=False)" I receive:

File "/home/douglas/Documentos/Django/my-second-blog/site_/app/views.py", line 59 post = form2.save(commit=False) ^ IndentationError: unindent does not match any outer indentation level

If I put the same expression but before of "post = form2.save(commit=False)" I receive:

File "/home/douglas/Documentos/Django/my-second-blog/site_/app/views.py", line 59 cont = 3 ^ TabError: inconsistent use of tabs and spaces in indentation

the code:

def evaluation(request):
    # form2 initialization
    list_evaluation = Evaluation.objects.all()
    form2 = EvalForm()
    cont = 0

    if request.method == "POST":
        form2 = EvalForm(request.POST)
        if form2.is_valid() == True:
            cont = 3
            post = form2.save(commit=False)
            post.save()
            return redirect('canditate_list')
    else:
        form2 = EvalForm()
    return render(request, 'app/evaluation.html', {'criterions': form2,})

[EDIT]: Guys, I don't indented wrong because I just press "Enter" before I type "True:",There's no way I could have mistaken it if the editor did this automatically

[EDIT2]: I NEVER use space to indent my codes precisely avoid this problems, I ALWAYS use "tab" and just "tab"

Douglas da Dias Silva
  • 1,142
  • 2
  • 10
  • 15

2 Answers2

2

It's all about indentation! Python determines what where the code is based on its indentation (rather than {} like most other languages).

Here's an example, this is valid code:

if 1 == 1:
    print('If passed!')

Once the if statement determines that 1 == 1 is True it looks for the indentation on the next line and runs it!

Here's another example, this is invalid code:

if 1 == 1:
print('If passed!')

Once the if statement determines that 1 == 1 is True it looks for the indentation on the next line, however, it doesn't find the indentation, so it doesn't know what to do, so it throws an IndentationError

In your code, you have this, and it works!

if request.method == "POST":
    form2 = EvalForm(request.POST)

My guess is that when you add the cont = 3 you add it like this

if request.method == "POST":
cont = 3
    form2 = EvalForm(request.POST)

So when python gets passed the if statement it looks for the next indentation, but there is none, so it throws an IndentationError.

I hope this helps you understand what's going on!

Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
  • Well friend, I understanding and know all your tell me, but I idled right, take a look in my code – Douglas da Dias Silva Mar 29 '17 at 22:17
  • 2
    Hum, it seems like it's a tabs vs spaces problem or something in that case. Could you edit your question to include the code that causes the error? – Aaron N. Brock Mar 29 '17 at 22:20
  • My guess would be there is some off character there. Try deleting the entire line and copying the ` form2 = EvalForm(request.POST)` line above and see if that works. – Aaron N. Brock Mar 29 '17 at 22:49
0

Normally, this happens because sometimes tabs != spaces. This also depends on type of editor a person is using.

A tab could be a different number of columns depending on your environment, but a space is always one column.

Spaces are the actual lowest-common denominator in punctuation.Many decent text editor has a "replace tabs with spaces" and this makes easier for programmers to just hit TAB key and get the desired indentation.

If you ever face such an error, try using spaces, because you can't go wrong with spaces, you might go wrong with tabs.

Latest editors like PyCharm follows PEP-8 and according to PEP-8, it's recommended to use spaces only for indentation in Python programs.