【发布时间】:2014-01-30 12:39:30
【问题描述】:
我正在尝试使用表单更新数据库中的现有对象。 我需要获得的是: 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)`
模型.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>
你认为我哪里错了?
提前谢谢..
编辑: 表格似乎无效。有什么想法吗?
【问题讨论】:
-
你读过 Django 的表单文档吗?他们非常好,实际上解释了如何做几乎正是你所追求的:docs.djangoproject.com/en/dev/topics/forms/modelforms
-
是的。我想要的是更新/修改一个已经存在的对象。不创建一个新的..