【发布时间】:2015-01-11 04:56:10
【问题描述】:
我有一个关于 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
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})
感谢您的帮助
【问题讨论】:
-
我使用的是 Django 1.8 版本
-
你可能想改用
nom = form.cleaned_data.get('nom')