美文网首页软件测试精进之路python_web开发
Django学习第一天之创建应用 生成数据表

Django学习第一天之创建应用 生成数据表

作者: 胆小的米老鼠 | 来源:发表于2018-08-13 17:20 被阅读1次

    上一篇我们学习如何配置Django开发环境,这一篇文章我们继续来学习Django的内容!

    我们已经创建了一个项目,大家还记得创建项目的命令吗?django-admin startproject + 项目名,我们接着来创建我们应用,命令为:python manage.py startapp + 应用名。查看一下应用的目录:

    ~/Desktop/projects/test1$ tree booktest/
    booktest/
    ├── admin.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py
    
    1 directory, 6 files
    

    接起来我们需要在booktest下的models.py文件中编写我应用模板:

    from django.db import models
    
    # Create your models here.
    class bookInfo(models.Model):
        btitle = models.CharField(max_length=20)
        bpub_data = models.DateField()
    
    class hreoInfo(models.Model):
        hname = models.CharField(max_length=10)
        hgender = models.BooleanField()
        hcontent = models.CharField(max_length=1000)
        hbook = models.ForeignKey(bookInfo)
    

    创建完模板我们在命令行运行一下,看看效果:

    ~/Desktop/projects/test1$ python manage.py runserver 8080
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have unapplied migrations; your app may not work properly until they are applied.
    Run 'python manage.py migrate' to apply them.
    
    August 13, 2018 - 08:55:09
    Django version 1.8.2, using settings 'test1.settings'
    Starting development server at http://127.0.0.1:8080/
    Quit the server with CONTROL-C.
    [13/Aug/2018 08:55:58]"GET / HTTP/1.1" 200 1767
    [13/Aug/2018 08:55:58]"GET /favicon.ico HTTP/1.1" 404 1935
    
    

    我们点击结果中提示的本地链接:


    网页显示效果

    生成数据表:

    第一步需要在settings.py文件中注册应用,

    注册应用

    第二步就是用命令行生成迁移文件:

    ~/Desktop/projects/test1$ python manage.py makemigrations 
    Migrations for 'booktest':
      0001_initial.py:
        - Create model bookInfo
        - Create model hreoInfo
    

    第三步就是执行迁移文件:

    ~/Desktop/projects/test1$ python manage.py migrate 
    Operations to perform:
      Synchronize unmigrated apps: staticfiles, messages
      Apply all migrations: booktest, sessions, auth, admin, contenttypes
    Synchronizing apps without migrations:
      Creating tables...
        Running deferred SQL...
      Installing custom SQL...
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying booktest.0001_initial... OK
      Applying sessions.0001_initial... OK
    

    总结:本篇文章主要是讲了,创建项目应用,编写我们应用模板,如何生成数据表(注册应用,生成迁移文件,执行迁移文件)的学习操作,希望对大家有帮助,Django学习是一个持续过程,我会记录我的学习内容教程,欢迎大家一起学习。

    相关文章

      网友评论

        本文标题:Django学习第一天之创建应用 生成数据表

        本文链接:https://www.haomeiwen.com/subject/fxpubftx.html