Django_2_ model layer

2019.5.29  

 

Today learning content: the model layer: model definition, django shell, django admin

######################################################################################################

3. acquaintance Django model layer:

  About layer model #

      . I What is:

        Django view layer and located between databases. Python convert between objects and database tables.

      . Ii Why:

        Shield differences between different databases. Developers are more focused on developing business logic. It offers many convenient tools to help operate the database.

      . Iii model layer configuration:

        >django_introduction>settings.py:

DATABASES={

  'default' = {

    'ENGINE': (what used database-driven)

    'NAME': (database name)

}

}

  

 

  # Create a blog post model

      i. model design blog

        Article Title: Text Type

        Abstract: text type

        Article content: text type

        Unique ID tag: Int digital type (auto-increment primary key)

        Date: Date Type

 

      ii. custom field model layer

        Digital Type: IntergerField

        Text Type: TextField

        Date Type: DataTimeField

        Incrementing ID: AutoField

        Primary key definition: primary_key properties

 

      iii blog post model definition:. > Blog> models.py  

class Article(models.Model):

  The only article ID #

  article_id = models.AutoField(primary_key=True)

  # Article Title

  title = models.TextField()

  Abstract # article

  brief_content = models.TextField()

  The main content of the article #

  content = models.TextField()

  Date # of the article (return real-time)

  publish_date = models.DateTimeField(auto_now=True)

 

Then enter the terminal " Python manage.py makemigrations " in> migrations in the migration file is created a model (0001--initial.py)

Enter " Python manage.py the migrate " to run the migration file to the database to synchronize data

 

( Note: If you want to modify models, while the second synchronous database title can not be empty if the first field is created title, was not affected, because the table is no existing rows in the data table for the first time have created and has been inserted in the data table, you must define a default value to populate an existing line, because otherwise it would violate the integrity of the database data without receiving data table changes. News 500:

  OperationalError: no such column: blog_article.title)

 

 

  # Django shell acquaintance

     What i. Django Shell is

        python shell: Python programming for interactive (relative to c, java programming compiler)

        Django shell: Similarly, inheritance python shell and use Django

 

     ii. 为什么需要Django shell  (方便开发,调试,debug)

        临时性操作使用Django shell更加方便

        小范围Debug更简单,不需要整个项目测试 

 

     iii. 使用

        在终端里输入“ python manage.py shell ” 进入Django shell 环境

新建Aritcle和保存:

from blog.models import Article

a = Article()

a.title = 'Test Django Shell '

a. brief_content = 'Test Django Shell, By Marvin'

a.content = 'Test Django Shell, New Ariticle, Main content.'

print(a)

a.save()

 

验证是否在数据库里:

articles = Article.objects.all()

article = articles[0]

print(article.title)

#打印title,逐个尝试#

……

 

 

  #. 初识Django Admin模块

      i. 是什么

        Django后台管理工具。

        读取定义的模型元数据,提供强大的管理使用页面

 

      ii. 为什么要用它

        Django shell 新增文章太复杂了

        管理页面是基础设施中重要的部分

        认证用户、显示管理模型、校验输入等功能

 

      iii. 使用:创建管理员用户,登陆界面进行创建

终端里输入“ python manage.py createsuperuser

用户名:marvin

邮箱地址:可以不填

密码:marvin123

 

输入“ python manage.py runserver

到浏览器页面,输入: https://127.0.0.1:8000/admin

输入用户密码,进入了admin的管理后台(one uesr, no group)

需要把模型注册到admin才能在后台看到:>blog>admin.py  输入:

from .models import Article

admin.site.register(Article)

回到网页后台刷新,多了个Aritcle,点入见到Article_object(每篇文章都是这个名字) 可改内容也可以创建文章。

可以对article内容进行修改:

我服了。。初学就踩雷,版本问题会导致admin object添加或修改失败,更新到django 2.1.5解决

 

如何在后台显示每个文章的title:

>blog>models.py  class Article 后面加入:

def __str__(self):

  return self.title

重新在终端输入 “ python manage.py runserver ”,回网页刷新,在后台每篇文章都显示自己的名字了。

 

  #. 实现博客数据返回页面(实战)

打开:>blog>views.py  新建一个视图函数实现功能:

from blog.models import Article

def article_content(request):

  article = Article.objects.all()[0]

  title = article.title

  brief_content = article.brief_content

  article_id = article.article_id

  publish_date = article.publish_date

  return_str = 'title:%s, brief_content:%s, content:%s, article_id:%s, publish_date:%s' %(title,brief_content,article_id,pulish_date)

  return HttpResponse(return_str)

 

应用级别的路由配置:打开 >blog>urls.py   在urlpaterns 中加入:

path('content',blog.views.article_content)

项目级别的路由配置: 打开 >django_introduction>urls.py ,之前已经配置过blog转发,这里不用再次配置

打开浏览器:网址加入/blog/content  展示了文章的详细情况:

 

 

######################################################################################################

 

Guess you like

Origin www.cnblogs.com/marvintang1001/p/10944543.html