user3330393
user3330393

Reputation:

django forms views.py - error : inconsistent use of tabs and spaces in indentation

please I have a problem concerning Gjango programming. I want to recover data from django forms to use it in views.py but i recieved an error : inconsistent use of tabs and spaces in indentation (views.py, line 21). i tried to use cleaned_data or request.POST i always find the same Error, here is my source code: models.py

from django.db import models

# Create your models here.

class Personne(models.Model):
    nom=models.CharField(max_length=200)
    prenom=models.CharField(max_length=200)

    def __unicode__(self):
        return self.nom
    def __str__(self):
        return self.nom

forms.py

from django import forms
from application1.models import Personne

class PersonneForm(forms.ModelForm):
        class Meta:
            model=Personne
            #fields = '__all__'
            fields=('nom','prenom')

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from application1.forms import PersonneForm
def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = PersonneForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:

            #I want to recover data here  i tried to use cleaned_data or request.POST and i find the same error
            nom = form.cleaned_data['nom']
            nom=request.POST['nom']

            form.save()


    # if a GET (or any other method) we'll create a blank form
    else:
        form = PersonneForm()

    return render(request, 'name.html', {'form': form})

Thank you for the help

Upvotes: 0

Views: 12705

Answers (2)

br3w5
br3w5

Reputation: 4593

Mixing tabs and spaces as a method of indentation in Python is illegal i.e. the Python interpreter will not accept it and your application will not run, as you've experienced. When indenting in Python you must use EITHER spaces OR tabs.

As to whether to use tabs or spaces, please refer to PEP8 (the Python coding style guide):

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

The important thing is to choose one style so that code is consistent and Guido chose spaces.

Related question.

Upvotes: 3

Rajesh Chamarthi
Rajesh Chamarthi

Reputation: 18818

This is a problem with your source file views.py and not with the application logic or the data you are passing from one function to another. In your text editor, search for tabs "\t" and replace them with " " (4 spaces).

enter image description here

Upvotes: 6

Related Questions