0

my django code shows me this error unindent does not match any outer indentation, read that it's about tabs and spaces, but nothing works for me. I will appreciate if someone take a look into my code.

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
# Create your views here.
from .models import *
from .forms import OrderForm
from .filters import OrderFilter

def home(request):
    orders = Order.objects.all()
    customers = Customer.objects.all()

    total_customers = customers.count()

    total_orders = orders.count()
    delivered = orders.filter(status='Delivered').count()
    pending = orders.filter(status='Pending').count()

    context = {'orders':orders, 'customers':customers,
    'total_orders':total_orders,'delivered':delivered,
    'pending':pending }

    return render(request, 'accounts/dashboard.html', context)

def products(request):

    products = Product.objects.all()

    return render(request, 'accounts/products.html', {'products':products})

def customer(request, pk_test):
    customer = Customer.objects.get(id=pk_test)

    orders = customer.order_set.all()
    order_count = orders.count()

    **myFilter = OrderFilter()**
    
    context = {'customer':customer, 'orders':orders, 'order_count':order_count, 'myFilter': myFilter}
    return render(request, 'accounts/customer.html',context)

def createOrder(request, pk):

    customer = Customer.objects.get(id=pk)
    form = OrderForm(initial={'customer': customer})
    if request.method == 'POST':
        #print('Printing POST:', request.POST)
        form = OrderForm(request.POST) #sending data into the form
    if form.is_valid():
        form.save()
        return redirect('/')


    context = {'form': form}
    return render(request, 'accounts/order_form.html', context)

def updateOrder(request, pk):

    #prefill forms after click update
    order = Order.objects.get(id=pk)
    form = OrderForm(instance=order)

    #save the changes
    if request.method == 'POST':
        form = OrderForm(request.POST, instance=order) #sending data into the form
    if form.is_valid():
        form.save()
        return redirect('/')

    context = {'form': form}
    return render(request, 'accounts/order_form.html', context)

def deleteOrder(request, pk):

    order = Order.objects.get(id=pk)

    if request.method == 'POST':
        order.delete()
        return redirect('/')
    context = {'item': order}
    return render(request, 'accounts/delete.html', context)

The error it shown is on the 37 line I will make it bold. Thank you for your time devs. Edit: tried to make it bold while it's in code mode but it shows it in **. The line is myFilter = OrderFilter() in customer function, where the error is shown. Tried python -m tabnanny views.py and it shows exactly the error message 'views.py': Indentation Error: unindent does not match any outer indentation level (, line 37)

  • copy paste your code from your ide to notepad++. Then click view/show symbol/show all characters. Then inspect your line 37 – Eric Martin Aug 21 '20 at 10:35
  • 1
    What editor are you using? If you use black to auto format, it should fix it. `pip install black` then `black .` – FlipperPA Aug 21 '20 at 10:56
  • And pycharm : select your code and the ctrl-shift-A then find "To Spaces" from there. – Eric Martin Aug 21 '20 at 11:10
  • I'm using VScode, forget to mention. –  Aug 21 '20 at 11:12
  • @EricMartin is there any alternatives for notepad++, because on ubuntu it fails. –  Aug 21 '20 at 11:23
  • I don't know alternatives. But if you re using VSCode, options are available to show all characters: https://stackoverflow.com/questions/6255839/show-space-tab-crlf-characters-in-editor-of-visual-studio.But have you try to rewrite carefully this part of code? – Eric Martin Aug 21 '20 at 12:07
  • You're mixing tabs and spaces. For VSCode look at this: https://stackoverflow.com/questions/33974681/how-can-i-convert-tabs-to-spaces-and-vice-versa-in-an-existing-file – Kevin Eaverquepedo Aug 21 '20 at 22:39

1 Answers1

0

I just fix it on VScode to convert tabs into spaces press F1, then Convert Indentation to Spaces