美文网首页
Python3 Web开发框架-Django学习笔记(二)创建应

Python3 Web开发框架-Django学习笔记(二)创建应

作者: 云一程 | 来源:发表于2017-04-11 11:15 被阅读707次

    python web开发学习心得,原创文章,转载请注明出处

    1.创建应用程序

    ~/Desktop/python/leaning_log ᐅ source ll_env/bin/activate
    (ll_env) ~/Desktop/python/leaning_log ᐅ python manage.py startapp learning_logs
    (ll_env) ~/Desktop/python/leaning_log ᐅ ls
    db.sqlite3    leaning_log   learning_logs ll_env        manage.py
    (ll_env) ~/Desktop/python/leaning_log ᐅ ls learning_logs
    __init__.py admin.py    apps.py     migrations  models.py   tests.py    views.py
    (ll_env) ~/Desktop/python/leaning_log ᐅ
    

    2.定义模型,创建一个名为Topic的类

    打开文件models.py,导入modes模块
    models.py
    --------------
    rom django.db import models
    
    # Create your models here.
    
    class Topic(models.Model):
        """用户学习的主题"""
        text = models.CharField(max_length = 200)
        date_added = models.DateTimeField(auto_now_add=True)
    
        #python2.7调用__unicode__()代替__str__()
        def __str__(self):
            '''返回摸醒的字符串展示'''
            return self.text
    

    3.激活模型

    1.打开settings.py(learning_log/learning_log里面)
    
    2.找到函数
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
    3.添加应用进去
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        
        #我的应用
        'learning_logs'
    ]
    

    4.修改数据库,使其能存储Topic相关信息

    1.命令makemigrations让Django确定如何修改数据库
    (ll_env) ~/Desktop/python/leaning_log ᐅ python3 manage.py makemigrations learning_logs
    Migrations for 'learning_logs':
      learning_logs/migrations/0001_initial.py:
        - Create model Topic
    (ll_env) ~/Desktop/python/leaning_log ᐅ
    
    2.让Django提我们修改数据库
    (ll_env) ~/Desktop/python/leaning_log ᐅ python3 manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, learning_logs, sessions
    Running migrations:
      Applying learning_logs.0001_initial... OK
    (ll_env) ~/Desktop/python/leaning_log ᐅ
    
    

    小结:每当需要修改管理的数据时:

    1.修改models.py
    2.对learnin_logs调用makemigrations
    3.让Django迁移项目
    

    3.创建超级用户

    用户名可用默认,邮箱可为空
    (ll_env) ~/Desktop/python/leaning_log ᐅ python manage.py createsuperuser
    Username (leave blank to use 'jinyulong'):
    Email address:
    Password:
    Password (again):
    Superuser created successfully.
    (ll_env) ~/Desktop/python/leaning_log ᐅ
    

    4.向管理网站注册模型

    from django.contrib import admin
    from learning_logs import  Topic
    
    admin.site.register(Topic)
    

    5.运行chrome查看效果

    管理员登录.png 管理员控制台.png

    6.添加主题

    点击Topics的Add,看见输入框,点Save,就保存了刚刚创建的主题

    相关文章

      网友评论

          本文标题:Python3 Web开发框架-Django学习笔记(二)创建应

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