Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unexpected indent" error when using notepad++ for creating django function

I am following this book to learn django using notepad++, something interesting happens, when I type the function using notepad++ for the following script:

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

It gives me an error like this:

IndentationError at /time/

('unexpected indent', ('M:\\DjangoStack\\projects\\beta_01\\..\\beta_01\\hello_world\\views.py', 12, 1, '\thtml = "<html>"\n'))

But when I paste it directly from the book, it is OK. I wonder why, should I do some settings in notepad++? Thanks.

update 01

I use 4 space bars to create the indent in notepad++, I tried 1 tab and seems the problem is fixed

like image 425
lokheart Avatar asked Dec 13 '11 03:12

lokheart


People also ask

How do you fix an unexpected indent error?

“IndentationError: unexpected indent” is raised when you indent a line of code too many times. To solve this error, make sure all of your code uses consistent indentation and that there are no unnecessary indents.

How do I get rid of unexpected indent in Python?

Just say "no" to tabs. Most editors allow them to be automatically replaced by spaces. The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.

Why am I getting unexpected indent in Python?

What is unexpected indent in Python? As the error message indicates, you have an indentation error . This error occurs when a statement is unnecessarily indented or its indentation does not match the indentation of former statements in the same block.

How do I fix IndentationError Unindent does not match any outer indentation level?

The Python "IndentationError: unindent does not match any outer indentation level" occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.


1 Answers

Tell Notepad++ to show you all characters: go View > Show Symbol > Show All Characters. This will show tabs as and spaces as . Replace tabs with spaces where necessary to normalize the indentation.

like image 95
Martin Green Avatar answered Oct 16 '22 06:10

Martin Green