我正在尝试使用表单从数据库更新现有对象。 我需要获得的是: 1显示要修改的数据,2,用新的数据更改数据3,显示新的数据。
我管理了第1步,但它是第2步的一个谜。你能帮我一把吗?
这是我的代码:
Views.py
`def modec(request,etatcivil_id): 如果不是request.user.is_authenticated(): return render_to_response('cv / connexionrequired.html')
k = request.user.email
if request.method == 'POST':
ec = Etatcivil.objects.get(id=etatcivil_id)
form = EtatCivilForm(data=request.POST, instance=ec)
form.auteur=k
print "Product Post"
if form.has_changed():
print "form has changed"
if form.is_valid():
print "Display Form"
form.save(commit=False)
fom.save()
return HttpResponseRedirect('/cv/creer')
else:
eta = Etatcivil.objects.get(id= etatcivil_id)
form = EtatCivilForm(eta.__dict__)
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('cv/modec.html', args)`
Model.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.conf import settings
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError('Users must have an email address')
if not username: username = email.split('@')[0]
user = self.model(
email= MyUserManager.normalize_email(email))
user.set_password(password)
user.save(using=cv._db)
class MyUser(AbstractBaseUser):
email = models.EmailField(max_length=254, unique=True, db_index=True)
registration = models.DateField(auto_now_add=True)
objects = MyUserManager()
USERNAME_FIELD = 'email'
class Meta:
unique_together = ('email', )
class Etatcivil(models.Model):
Auteur = models.ForeignKey(settings.AUTH_USER_MODEL, 'email')
Nom = models.CharField(max_length=70)
Prenom = models.CharField(max_length=70)
Date_de_Naissance = models.DateTimeField()
Email = models.CharField(max_length=70)
Telephone = models.CharField(max_length=15)
Reseaux = models.CharField(max_length=30)
和modec.html
<!DOCTYPE html>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'cv/style/style.css' %}" />
<html style="width: 100%; min-width:50%;">
<head>
<meta charset="utf-8" />
<title> Créer votre CV</title>
</head>
<body>
<header>
<table>
<tr style=" topmargin: 0px; leftmargin: 0px ">
<td style="padding: 0px; topmargin: 0px; leftmargin: 0px; width=70% ">
<img src=" {% static "cv/photos/logo.png" %}"/>
</td>
<td class="menutd">
<a style="color: #404040;" href="/cv/creer"> Enregistrer mes Informations </a>
</td>
<td class="menutd">
{% block content %}
<a style="color: #404040;" href="/cv/generer"> Generer un CV </a>
</td>
<td class="menutd">
<a style="color: #404040;" href="/login/off" align="left"> Deconnection </a>
</td>
</tr>
</table>
</header>
<div style=" width: 50%; Height: 100%; line-height: 100%; margin-top: 150px; margin-bottom: auto; margin-left: auto; margin-right: auto; vertical-align: middle; text-align: center; font-family: Helvetica Neue; font-size: 36px; color: #ffffff">
<form action="" method="POST"> {% csrf_token %}
{% for field in form %}
<input type="{{ field.Charfield }}" name="{{ field.name }}" value="{{ field.value }}"> </input><br />
{% endfor %}
<input type="submit" name="valider mon etat civil" value="Valider Article">
</form>
{% endblock %}
</div>
</div>
</body>
</html>
你认为我哪里错了?
提前致谢..
编辑: 看来该表格无效。有什么想法吗?
答案 0 :(得分:0)
我不知道你为什么认为在实例化它时需要将实例的__dict__
属性传递给表单。这是问题的原因,因为表单的第一个参数是data
参数,并且传递使表单绑定 - 触发验证,正如文档中详细解释的那样。
而不是这样做,你应该做你在POST块中正确完成的事情:传递实例。
eta = Etatcivil.objects.get(id=etatcivil_id)
form = EtatCivilForm(instance=eta)
还要注意你在模板中做了一些非常奇怪的事情({{ form.CharField }}
应该是什么?)但是你没有做的一件事是显示表单错误,你应该这样做 - 否则你赢了不知道为什么表格无效。同样,所有这些都在文档中详细解释。