从ModelForm保存数据 [英] Saving data from ModelForm

查看:48
本文介绍了从ModelForm保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,我正在尝试使用ModelForm保存数据.模板"Vlozit"具有一个ModelForm,提交时,我希望将数据保存在数据库中,并重定向到base.html,该数据库实际上是从数据库加载数据并列出输出.问题是,一切正常,但未保存数据.请帮助我找到我所缺少的.谢谢.

I am new to Django and I'm trying to save data using ModelForm. Template 'Vlozit' has a ModelForm and when submitted, I want the data saved in the DB and the redirect to base.html that actually loads the data from the DB and lists the output. The problem is that all works fine but the data is not saved. Please help me find what I am missing. Thank you.

这是模型:

class Customer(models.Model):
    Name = models.CharField(max_length=30)
    Description = models.CharField(max_length=150)
    Creation_Date = models.DateField(default=datetime.now)
    Active = models.BooleanField(default=False)

def __unicode__(self):
    return self.Name

Customers = models.Manager()

这是ModelForm:

Here's the ModelForm:

class CustomerForm(forms.ModelForm):
    Description = forms.CharField(widget=forms.Textarea)

    class Meta:
        model = Customer

这里是视图:

def vlozit(request):
    if request.method == 'POST':
        form = CustomerForm(request.POST, instance=Customer)
        if form.is_valid():
            form.save(True) 
            return HttpResponseRedirect(reverse('Base.html'))
    else:
        form = CustomerForm()
        return render_to_response("Vlozit.html", {'form': form}, context_instance = RequestContext(request))

这是模板"Vlozit":

Here's the template 'Vlozit':

{% extends "base.html" %}

{% block head %}
    {{ form.media }}
    <script>
        $(function() {
            $( "#id_Creation_Date" ).datepicker();
        });
    </script>
{% endblock %}

{% block title %}{{block.super}} - Vlozit{% endblock %}
{% block content %}
<div id="content">
    <form method="POST" action="{% url url_home %}">
    {% csrf_token %}
        <table>
            <tr>
                <td>Name</td>
                <td>{{ form.Name }}</td>
            </tr>
            <tr>
                <td>Description</td>
                <td>{{ form.Description }}</td>
            </tr>
            <tr>
                <td>Creation Date</td>
                <td>{{ form.Creation_Date }}</td>
            </tr>
            <tr>
                <td>Active</td>
                <td>{{ form.Active }}</td>
            </tr>
        </table>
        <input type="submit" value="Vlozit">
    </form>
</div>
{% endblock content %}

推荐答案

正如Timmy在评论中所说,您不会发现表格无效的情况.如果 form.is_valid()为False,您不仅不会在模板中显示错误,而且甚至不会重新显示模板.将视图的最后一行移回一个缩进级别,然后将 {{form.errors}} 添加到模板中.

As Timmy says in the comments, you don't catch the case where the form is not valid. Not only do you not show errors in the template, but you also don't even redisplay the template if form.is_valid() is False. Move the last line of the view back one indentation level, and add {{ form.errors }} to the template.

这篇关于从ModelForm保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆