• 四、Django学习:定义后台与修改模型


    默认Django后台管理页面无法看出标题,需要进行一定的配置。

    在修改模型(增加、删除字段)时,需要设置默认值,而且需要更新数据库使应用生效。

    一、定制后台页面

    • 修改article models.py文件
    from django.db import models
    
    # Create your models here.
    
    class Article(models.Model):
        title = models.CharField(max_length=40)
        content = models.TextField()
        
        def __str__(self):
            return "<Article:%s>" % self.title
    

    启动服务python manage.py runserver,访问localhost:8000/admin,输入账号密码。

    要让内容显示出来,不建议在models.py文件中进行修改,而是应该在admin.py文件中进行修改。

    • 修改article admin.py文件
    from django.contrib import admin
    from .models import Article
    
    # Register your models here.
    class ArticleAdmin(admin.ModelAdmin):
        # 显示字段
        list_display = ("id", "title", "content")
       
    admin.site.register(Article, ArticleAdmin)
    

    重新启动服务,进入后台页面。

    调整显示顺序

    修改article admin.py文件

    from django.contrib import admin
    from .models import Article
    
    # Register your models here.
    
    class ArticleAdmin(admin.ModelAdmin):
        list_display = ("id", "title", "content")
        # 正序排列,如果需要按相反顺序,在前面加-号,如ordering = ("-id", )
        ordering = ("id", "title", "content")
    
    admin.site.register(Article, ArticleAdmin)
    
    • 另外一种注册的方式

    修改article admin.py文件

    from django.contrib import admin
    from .models import Article
    
    # Register your models here.
    @admin.register(Article)
    class ArticleAdmin(admin.ModelAdmin):
        list_display = ("id", "title", "content")
        ordering = ("id", "title", "content")
        
    

    保存刷新,结果一样。


    二、修改模型

    修改article models.py文件

    from django.db import models
    #from django.utils import timezone
    from django.contrib.auth.models import User
    
    # Create your models here.
    
    class Article(models.Model):
    	title = models.CharField(max_length=40)
    	content = models.TextField()
    	#created_time = models.DateTimeField(default=timezone.now)
    	created_time = models.DateTimeField(auto_now_add=True)
    	last_updated_time = models.DateTimeField(auto_now=True)
    	author = models.ForeignKey(User, on_delete='models.DO_NOTHING', default=1)
    	is_deleted = models.BooleanField(default=False)
    	readed_num = models.IntegerField(default=0)
    	
    	def __str__(self):
    		return "<Article:%s>" % self.title
    

    修改article admin.py文件

    from django.contrib import admin
    from .models import Article
    
    # Register your models here.
    @admin.register(Article)
    class ArticleAdmin(admin.ModelAdmin):
    	list_display = ("id", "title", "author", "content", "is_deleted", "created_time", "last_updated_time")
    	ordering = ("id", )
    
    #admin.site.register(Article,ArticleAdmin)
    

    执行数据库迁移及迁移应用操作,重启服务

    ​ 标识is_deleted用法:被标识为删除的文章并没有实际删除,可以通过修改views.py文件来过滤显示,让127.0.0.1:8000/article只显示未被标记删除的文章列表。

    • 修改views.py文件
    from django.shortcuts import render,get_object_or_404,render_to_response
    from django.http import HttpResponse,Http404
    from .models import Article
    
    # Create your views here.
    
    def article_detail(request, article_id):
    	#article = Article.objects.get(id=article_id)
    	article = get_object_or_404(Article,pk=article_id)
    	context = {}
    	context['article_obj'] = article
    	return render_to_response('article_detail.html', context)
    	
    def article_list(request):
        # 使用Article.objects.filter()来进行过滤
    	article_list = Article.objects.filter(is_deleted=False)
    	context = {}
    	context['article_list_obj'] = article_list
    	return render_to_response('article_list.html', context)
    

    重新执行迁移,重启服务,可以看到标记为删除的文章标题未显示出来。

  • 相关阅读:
    Oracle的启动过程共经历四个阶段:
    Oracle数据库启动原理
    Oracle LISTENER监听文件参数详解及Lsnrctl命令综述
    Oracle TNS简述
    linux 各级目录的作用
    oracle 监听器的工作原理
    插入标识列
    sql server日期格式转换方法大全
    html获取输入的值的问题
    关闭窗口 不弹出提示
  • 原文地址:https://www.cnblogs.com/sjfeng1987/p/11352795.html
Copyright © 2020-2023  润新知