0

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

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

但它不会提交给数据库?

这是我的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,
    })
4

1 回答 1

1

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

表格.py

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

视图.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,
    })
于 2012-12-05T05:26:07.780 回答