开启代码之旅 Launch With Code


1378 浏览 5 years, 11 months

6 Start a Django

版权声明: 转载请注明出处 http://www.codingsoho.com/

本节将介绍如果创建一个django应用,包括

  • model & db
  • admin
  • form

创建application

python manage.py startapp joins

创建model

joins/models.py

# Create your models here.
class Join(models.Model):
    email =  models.EmailField()
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    updated = models.DateTimeField(auto_now_add = False, auto_now = True)

    def __unicode__(self):
        return "%s " %(self.email)

生成数据表

python manage.py syncdb

将会创建joins_join表
需执行这个命令,否则user auth里找不到对应的model

创建admin接口

Joins\admin.py

from django.contrib import admin
from .models import Join

class JoinAdmin(admin.ModelAdmin):
    list_display = ['__unicode__', 'timestamp', 'updated']
    class Meta:
        model = Join

admin.site.register(Join, JoinAdmin)