首页 > 代码库 > Django之modelform

Django之modelform

在django中内置了form类和model类,当页面中的form值和model字段值完全一样时,此时可以通过model生成一个完全一样的form,Django中的modelForm就因此而生。

目标:

  定义Author表,通过modelform提交数据到数据库

 

创建工程及app

python django-admin.py startproject formstudy
cd formstudy
python manager.py startapp study

 

编辑models.py

from __future__ import unicode_literals

from django.db import models

# Create your models here.

TITLE_CHOICES = (
    (‘MR‘,‘Mr.‘),
    (‘MRS‘,‘Mrs.‘),
    (‘MS‘,‘Ms.‘),
)

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3,choices=TITLE_CHOICES)
    birth_date = models.DateField(blank=True,null=True)

    def __unicode__(self):
        return self.name

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

 

在app目录下新建forms.py

技术分享

 

 修改views.py

技术分享

 

修改urls.py

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^testModelform/$‘, views.testModelForm, name=‘testModelForm‘),
]

 

在templates目录下创建testModelForm.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Mode测试</title>
</head>
<body>
    <form method="post" action=".">{% csrf_token %}
        <table>
            <tr>
                <td>
                    <label for="id_name">作者名:</label>
                </td>
                <td>
                    {{ form_info.name }}
                </td>
                <td>
                    {{ form_info.name.errors }}
                </td>
            </tr>
            <tr>
                <td>
                    <label for="id_title">标题:</label>
                </td>
                <td>
                    {{ form_info.title }}
                </td>
                <td>
                    {{ form_info.title.errors }}
                </td>
            </tr>
            <tr>
                <td>
                    <label for="id_title">出生日期:</label>
                </td>
                <td>
                    {{ form_info.birth_date }}
                </td>
                <td>
                    {{ form_info.birth_date.errors }}
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="http://www.mamicode.com/注册">
                </td>
                <td>
                    <input type="reset" value="http://www.mamicode.com/重置">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 

修改admin.py

from django.contrib import admin
import models
# Register your models here.
class AuthorAdmin(admin.ModelAdmin):
    list_display = [‘name‘,‘title‘,‘birth_date‘]

admin.site.register(models.Author)

 

修改settings.py

INSTALLED_APPS = [
    ‘django.contrib.admin‘,
    ‘django.contrib.auth‘,
    ‘django.contrib.contenttypes‘,
    ‘django.contrib.sessions‘,
    ‘django.contrib.messages‘,
    ‘django.contrib.staticfiles‘,
    ‘study‘,
]

 

启动服务

python manage.py runserver 127.0.0.1:8001

 

效果图

技术分享

 

此时可以通过表单提交数据到数据库了

Django之modelform