我正在尝试使用django的FormPreview,但无法让它正常工作。这是我的代码:
models.py
class jobpost(models.Model):
CONTENT_CHANNELS = (
('Full Time','Full Time'),
('Part Time','Part Time'),
('Contract','Contract'),
)
user = models.ForeignKey(User)
job_id = models.AutoField(primary_key=True)
#user = models.ForeignKey(User, editable = False)
job_type = models.CharField(max_length=255,null=True, choices=CONTENT_CHANNELS,default='Full Time')
job_location = models.CharField(max_length=255,null=True)
job_title = models.CharField(max_length=255,null=True)
job_description = models.TextField(null=True)
def __unicode__(self):
#return self.user
return self.job_location
return self.job_type
return self.job_title
return self.job_description
admin.site.register(jobpost)
class jobpostForm(ModelForm):
class Meta:
model = jobpost
fields = ('user','job_type','job_location','job_title','job_description')
widgets = {
'job_type':RadioSelect(),
'job_location':TextInput(attrs={'size':'70'}),
'job_description':Textarea(attrs={'cols':200, 'rows':10}),
}
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = 'horizontal-form'
self.helper.form_id = 'id-jobpostform'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
#self.helper.form_action = '/'
self.helper.add_input(Submit('submit_addcontent', 'Submit'))
super(jobpostForm, self).__init__(*args, **kwargs)
views.py
def main_page(request):
"""
If users are authenticated, direct them to the main page. Otherwise, take
them to the login page.
"""
if request.method == 'POST':
form = jobpostForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/thanks/')
else:
form = jobpostForm()
c = {}
c.update(csrf(request))
return render_to_response('portal/job_post.html',{'form':form},context_instance=RequestContext(request))
preview.py
class SomeModelFormPreview(FormPreview):
form_template = 'portal/job_post.html'
preview_template = 'portal/preview.html'
def done(self, request, cleaned_data):
f = self.form(request.POST)
f.save()
pdb.set_trace()
print "done"
# Do something with the cleaned_data, then redirect
# to a "success" page.
return HttpResponseRedirect('/form/success')
job_post.html
<form action = "" method = "POST" enctype="multipart/form-data" class="blueForms" id="id-jobpostform">
{% csrf_token %}
{% crispy form %}
<input name="submit" type="submit" value="Post" >
urls.py
(r'^ $',main_page), (r'^ post / $',SomeModelFormPreview(jobpostForm)),
当我提交表单时,它会转到/ home页面并提交信息。我想首先查看表单然后提交..plz告诉我我错过了什么或做错了。