Skip to main content
 首页 » 编程设计

python之django 表单 views.py之错误 : inconsistent use of tabs and spaces in indentation

2025年05月04日26davidwang456

请问我有关于 Gjango 编程的问题。我想从 Django 表单中恢复数据以在 views.py 中使用它,但我收到一个错误:缩进中制表符和空格的使用不一致(views.py,第 21 行)。 我尝试使用 cleaned_data 或 request.POST 我总是发现相同的错误,这是我的源代码: 模型.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 

表单.py

from django import forms 
from application1.models import Personne 
 
class PersonneForm(forms.ModelForm): 
        class Meta: 
            model=Personne 
            #fields = '__all__' 
            fields=('nom','prenom') 

View .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}) 

谢谢你的帮助

请您参考如下方法:

这是源文件 views.py 的问题,而不是应用程序逻辑或从一个函数传递到另一个函数的数据的问题。在您的文本编辑器中,搜索制表符“\t”并将其替换为“”(4 个空格)。