0

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

Rajesh Chamarthi
  • 18,568
  • 4
  • 40
  • 67

2 Answers2

6

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

Rajesh Chamarthi
  • 18,568
  • 4
  • 40
  • 67
  • First thank you for the help, please could you be more explicit I'm new to Django programming. –  Nov 13 '14 at 22:15
  • Hi Essen. Added an image of how I deal with this problem usually. Most cases, it is tough to distinguish, since spaces and tabs look the same in the editor. – Rajesh Chamarthi Nov 13 '14 at 22:17
  • I guess this is not the problem because i put cleaned_data with form.save() at the same level and i put cleaned_data as comment and form.save() worked successfuly by storing in database. beg pardon –  Nov 13 '14 at 22:24
  • from our comments to the other answer, it looks like you were using tabs and indentations together. The "form referenced before assignment" is indication that there is a problem with the actual logic. You might want to create a new question with the line number in the error or edit your question to include the changed code and the line numbers of the error. – Rajesh Chamarthi Nov 13 '14 at 23:07
3

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.

Community
  • 1
  • 1
br3w5
  • 4,403
  • 5
  • 33
  • 42
  • Hi, thank you for this answer, i changed the tabs by spaces and i have the same problem –  Nov 13 '14 at 22:31
  • i comment cleaned_data['nom'] and the application works successfuly –  Nov 13 '14 at 22:32
  • after you changed the tabs for spaces did you still get the same error? – br3w5 Nov 13 '14 at 22:35
  • yeah, the same problem –  Nov 13 '14 at 22:39
  • Ok then that line that you commented must be tabbed if you're getting the same error – br3w5 Nov 13 '14 at 22:40
  • i deleted all the comments and i use spaces, the same problem and I'm sorry for that –  Nov 13 '14 at 22:43
  • i don't know if there is problem with notepad++ i use this editor –  Nov 13 '14 at 22:46
  • If you've tried the find and replace rajesh chamarthi recommended and that hasn't worked then you may need to manually remove all of the indentation in the get_name method in your views.py then add the spaces back in – br3w5 Nov 13 '14 at 22:53
  • thats good now but i found an other error : local variable 'form' referenced before assignment –  Nov 13 '14 at 22:59
  • ok you should create a new question for that and include the line the error is occurring on – br3w5 Nov 13 '14 at 23:05
  • thank you i solved the problem you gave me the main answer for it –  Nov 13 '14 at 23:18