在自定义HTML中提交Django表单

时间:2012-12-04 06:24:02

标签: django-forms

我是django表单的新手,我正在尝试创建一个非常具体的用户界面,所以我不能使用模型表单(或者我可以吗?)

我创建了一个如下所示的自定义表单:

但是它不会提交给db?

这是我的html表单:

    <form method="POST" action='/create/' id="big_idea_form" class="tabscontent">{% csrf_token %}
        <label class="tabpage" id="tabpage_1" ><span>next</span><p>Give it a name!</p><input type="text" id="name"  placeholder="Type your title here" onblur='changeIt("name", "name_span")'/></label>
        <label class="tabpage" id="tabpage_2" ><p>What is it about?</p><input type="text" id="subtitle" name="subtitle" placeholder="Type your subtitle here" onblur='changeIt("subtitle", "subtitle_span")'/></label>
        <label class="tabpage" id="tabpage_3" ><p>Why is it important?</p><textarea id="description" name= "description" cols="42" rows="5" placeholder="this can be any description but it becomes more interesting if you can explain why it is important as part of your description" onblur='changeIt("description", "description_span")'></textarea></label>
        <label class="tabpage" id="tabpage_4" ><p id="success_title">What is  considered success?</p><textarea id="success" name="success" cols="42" rows="5" placeholder="describe what sucess means to your students... what does mastery look like?" onblur='changeIt("success", "success_span")'></textarea></label>
        <input type="submit" value="Submit"/>        
    </form>  

这是我的表格:

from django import forms
import urlparse
from models import Lesson

class CreateLesson(forms.Form):
    name=forms.CharField()
    subtitle=forms.CharField(required=False)
    creator=forms.CharField() #automate
    description=forms.CharField()
    success=forms.CharField()
    unit=forms.CharField(required=False)
    public=forms.BooleanField()

    def save(self, request):
    data=self.cleaned_data
    Lesson.creator = request.user
    Lesson.name=data.get('name', '')
    Lesson.subtitle=data.get('subtitle', '')
    Lesson.topic=data.get('topic', '')
    Lesson.description=data.get('description', '')
    Lesson.unit=data.get('unit', '')
    Lesson.public=data.get('public', True)
    Lesson.save()
    return Lesson

以下是我的观点:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from forms import CreateLesson

def Create_Lesson(request):
    if request.method == 'POST': # If the form has been submitted...
        form = CreateLesson(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            form.save()
            return HttpResponseRedirect('/create/') # Redirect after POST
    else:
        form = CreateLesson() # An unbound form

    return render(request, 'create_lesson/new_ideas.html', {
        'form': form,
    })

1 个答案:

答案 0 :(得分:1)

问题是表单没有验证,因为模型中的字段没有在表单上显示... 我使用ModelForm简化了表单并将其排除为

forms.py

class CreateLessonModel(forms.ModelForm):
    class Meta:
    model = Lesson
    exclude =('unit', 'slug', 'topic', 'public', 'created_date', 'creator')

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from forms import CreateLessonModel
from django.template.defaultfilters import slugify
def Create_Lesson(request):
    if request.method == "POST": # If the form has been submitted...
        form = CreateLessonModel(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            lesson = form.save(commit=False)
            lesson.creator = request.user
            lesson.slug = slugify(lesson.name)
            lesson.save()
            return HttpResponseRedirect('') # Redirect after POST
    else:
        form = CreateLessonModel() # An unbound form

    return render(request, 'create_lesson/new_ideas.html', {
        'form': form,
    })