我对Django表单有一个奇怪的问题。有这个表单,我可以根据单击提交按钮完成不同的操作。在开发的某个阶段,一切都运行良好:我的表单验证正常,它正在做我想要的。但是现在(我真的不知道我可以做错什么来打破它),提交动作就行不通了。我没有收到任何错误,它什么也没做。
以下是我的forms.py文件中的代码:
class AssetModif(forms.Form):
def __init__(self, *args, **kwargs):
proj_id = kwargs.pop('proj_id')
super(AssetModif, self).__init__(*args, **kwargs)
self.proj_id = proj_id
EPISODE_CHOICES = [('-1', '------------')]
for e in Project.objects.filter(proj_id_parent=proj_id).order_by('proj_saison', 'proj_numepisode'):
EPISODE_CHOICES.append((e.proj_name,
"e" + str(e.proj_saison).zfill(2) + str(e.proj_numepisode).zfill(2) +
"_" + e.proj_name))
self.fields['episodes'].choices = EPISODE_CHOICES
self.fields['episode_etat'].choices = EPISODE_CHOICES
TYPE_CHOICES = [(type.type_name, type.type_name) for type in Type.objects.all()]
ASSET_CHOICES = (('', '-- choisir un type en premier'))
type = forms.ChoiceField(choices=TYPE_CHOICES,
widget=forms.Select(attrs={'onchange':'get_asset_from_type();'}))
asset = LazyChoiceField(choices=ASSET_CHOICES, required=False)
episodes = forms.ChoiceField()
etat = forms.CharField()
episode_etat = forms.ChoiceField()
commentaire = forms.CharField(widget=forms.Textarea)
还有一些save_action()方法,如果有人问我可以添加。我的views.py中的代码:
def modif_asset(request):
try:
proj = request.session['proj']
nom = request.session['nom']
except:
request.session['proj'] = 1
proj = request.session['proj']
request.session['nom'] = ""
nom = ""
if request.method == 'POST' and (request.POST.get("action_add_episode", "") == "Ajout Episode"):
form = AssetModif(request.POST, proj_id = request.session['proj'])
if form.is_valid():
asset = form.save_episode()
#return HttpResponseRedirect(reverse('menu'))
return render_to_response("asset/modif.html", {'proj':proj, 'form':form, 'asset':asset}, context_instance=RequestContext(request))
else:
asset = form.save_episode()
return render_to_response("asset/modif.html", {'proj':proj, 'form':form, 'asset':"toooooooo"}, context_instance=RequestContext(request))
elif request.method == 'POST' and (request.POST.get("action_delete_asset", "") == "Suppression BDD"):
form = AssetModif(request.POST, proj_id = request.session['proj'])
if form.is_valid():
asset = form.delete_asset()
#return HttpResponseRedirect(reverse('menu'))
return render_to_response("asset/modif.html", {'proj':proj, 'form':form, 'asset':asset}, context_instance=RequestContext(request))
elif request.method == 'POST' and (request.POST.get("action_ajout_etat", "") == "Ajout Etat"):
form = AssetModif(request.POST, proj_id = request.session['proj'])
if form.is_valid():
asset = form.save_etat()
return render_to_response("asset/modif.html", {'proj':proj, 'form':form, 'asset':asset}, context_instance=RequestContext(request))
elif request.method == 'POST' and (request.POST.get("action_ajout_comment", "") == "Ajout Commentaire"):
form = AssetModif(request.POST, proj_id = request.session['proj'])
if form.is_valid():
asset = form.save_comment()
return render_to_response("asset/modif.html", {'proj':proj, 'form':form, 'asset':asset}, context_instance=RequestContext(request))
else:
form = AssetModif(proj_id=request.session['proj'])
c = {'proj': proj, 'form':form, 'asset':" Miiii "}
c.update(csrf(request))
return render_to_response("asset/modif.html", c, context_instance=RequestContext(request))
我的模板是这样的:
<form method="POST" action="" name="form_modif">{% csrf_token %}
<table>
<tr><td> Type </td><td> {{ form.type }} </td>
<td> Asset </td><td> {{ form.asset }} </td></tr>
</table>
<div id="ajout_episode">
<p><b> Premier épisode de l'asset </b></p>
<table>
<tr><td>Episode</td><td> {{ form.episodes }}</td></tr>
<tr><td><input type="submit" name="action_add_episode" value="Ajout Episode" /></td></tr>
</table>
</div>
<div id="creation_etat">
<p><b> Ajout d'un état </b></p>
<table>
<tr><td>Etat</td><td> {{ form.etat }}</td></tr>
<tr><td>Premier épisode de l'état</td><td> {{ form.episode_etat }}</td></tr>
<tr><td><input type="submit" name="action_ajout_etat" value="Ajout Etat" /></td></tr>
</table>
</div>
<div id="ajout_comment">
<p><b> Ajout d'un commentaire </b></p>
<table>
<tr><td> {{ form.commentaire }}</td></tr>
<tr><td> <input type="submit" name="action_ajout_comment" value="Ajout Commentaire" /></td></tr>
</table>
</div>
<div id="suppression_bdd">
<p><b> Suppression de l'asset en BDD </b></p>
<input type="submit" onclick="return verifier();" name="action_delete_asset" value="Suppression BDD" />
</form>
我非常感谢任何帮助,因为我完全坚持了它
答案 0 :(得分:0)
不是一个答案太多,而是关于获取错误的指示。
您的第一个起点应该是查找字段错误,您目前根本没有输出它们。
所以,如果你这样做
<div id="ajout_episode">
<p><b> Premier épisode de l'asset </b></p>
<table>
<tr><td>Episode</td><td> {{ form.episodes }}</td></tr>
<tr><td><input type="submit" name="action_add_episode" value="Ajout Episode" /></td></tr>
</table>
{{form.episodes.errors}}
</div>
然后它将显示该字段的错误。 您可以使用以下代码段来检查错误:http://djangosnippets.org/snippets/1094/
表单也有一个全局错误变量,但我目前无法找到或记住它的名称。
答案 1 :(得分:0)
<form method="POST" action="url_to_your_view" name="form_modif">