我有一个包含2个表单的模板。我想使用“prefix”参数在我的渲染模板中使用不同的id:
我的第一张表格:
<input id="id_folder-name" maxlength="75" name="folder-name" type="text" />
我的第二种形式:
<input id="id_file-name" maxlength="75" name="file-name" type="text" />
我的观点,我实例化表格:
self.form_class(prefix="folder")
self.form_class_upload(prefix="file")
但它不起作用,请参阅控制台日志:
[23/Aug/2013 11:15:57] "GET /sample/ HTTP/1.1" 200 1037
invalid
<tr><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input id="id_name" maxlength="75" name="name" type="text" /></td></tr>
[23/Aug/2013 11:16:50] "POST /sample/new-folder/ HTTP/1.1" 200 634
所以它不会调用我的form_valid方法...
我不明白为什么Django不考虑验证表单的'prefix'参数。
models.py:
from django.db import models
class Inode(models.Model):
name = models.CharField(max_length=75)
file = models.FileField(upload_to="files", null=True)
def __unicode__(self):
return self.name
forms.py:
from django import forms
from .models import Inode
class FileUploadForm(forms.ModelForm):
class Meta:
model = Inode
fields = ['name', 'file']
class NewFolderForm(forms.ModelForm):
class Meta:
model = Inode
fields = ['name']
views.py:
from django.views.generic import TemplateView
from django.views.generic.edit import FormView
from .forms import FileUploadForm, NewFolderForm
from .models import Inode
class MainView(TemplateView):
template_name = "sample/browser.html"
form_class = NewFolderForm
form_class_upload = FileUploadForm
def get_context_data(self, **kwargs):
context = super(MainView, self).get_context_data(**kwargs)
context['form_new_folder'] = self.form_class(prefix="folder")
context['form_upload'] = self.form_class_upload(prefix="file")
context["inodes"] = [i for i in Inode.objects.all()]
return context
class NewFolderView(FormView):
template_name = "sample/browser.html"
form_class = NewFolderForm
success_url = "/sample"
def form_invalid(self, form):
invalid = super(NewFolderView, self).form_invalid(form)
print "invalid"
print form
return invalid
def form_valid(self, form):
isvalid = super(NewFolderView, self).form_valid(form)
print "valid"
return isvalid
模板:
<!DOCTYPE html>
<html lang='fr' xml:lang='fr'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sample</title>
</head>
<body>
<form method="post" action="{% url 'sample-new-folder' %}">
{% csrf_token %}
{{form_new_folder}}
<input type="submit" value="Valider"/>
</form>
<form method="post" action="{% url 'sample-upload' %}">
{% csrf_token %}
{{form_upload}}
<input type="submit" value="Valider"/>
</form>
</body>
</html>
渲染模板:
<html lang='fr' xml:lang='fr'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sample</title>
</head>
<body>
<form method="post" action="/sample/new-folder/">
<input type='hidden' name='csrfmiddlewaretoken' value='btopdI8WyHgqG2ZSTG81yhmaDa9Rk7Is' />
<tr><th><label for="id_folder-name">Name:</label></th><td><input id="id_folder-name" maxlength="75" name="folder-name" type="text" /></td></tr>
<input type="submit" value="Valider"/>
</form>
<form method="post" action="/sample/upload/">
<input type='hidden' name='csrfmiddlewaretoken' value='btopdI8WyHgqG2ZSTG81yhmaDa9Rk7Is' />
<tr><th><label for="id_file-name">Name:</label></th><td><input id="id_file-name" maxlength="75" name="file-name" type="text" /></td></tr>
<tr><th><label for="id_file-file">File:</label></th><td><input id="id_file-file" name="file-file" type="file" /></td></tr>
<input type="submit" value="Valider"/>
</form>
</body>
</html>