• 用django搭建一个简易blog系统(翻译)(三)


    06. Connecting the Django admin to the blog app

      Django 本身就带有一个应用叫作Admin,而且它是一个很好的工具

      在这一部分,我们将要激活admin应用,再次同步数据库和model,并且把admin和你的blog应用连接起来。

    07. 激活admin

      返回到settings.py文件,把INSTALLED_APPS改为像下面这样子:

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'blog',
        # Uncomment the next line to enable the admin:
        'django.contrib.admin',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
    )

      回到运行服务器的命令行中断,确认没有任何错误产生。如果没有错误,刷新你的浏览器确认项目中的其它地方也没有错误产生。

      经常检查是否有错误产生是一个很好的习惯。直到你用TTD开发方式(Test Driven Development)。现在你只要记着查看命令行中断和刷新浏览器来查看有没有错误就好了。

      如果没有错误产生,那么继续。

      接下来,打开urls.py文件,并且编辑它让它像下面这样:

    from django.conf.urls import patterns, include, url
    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/',include(admin.site.urls)),
        # Examples:
        # url(r'^$', 'netmag.views.home', name='home'),
        # url(r'^netmag/', include('netmag.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        # url(r'^admin/', include(admin.site.urls)),
    )

      再测试一次,检查命令行终端和浏览器,看有没有错误产生。如果没有错误产生,再同步一次数据库。你需要这么做,是因为你上次运行syncdb命令的时候,还没有配置使用admin应用。现在可以再运行一次syncdb命令,如果你留意,你会发现这个命令创建了新的表。

      

    python manage.py syncdb
    

      

    现在,你可以返回到浏览器,在地址栏输入http://127.0.0.1:8000/admin/, 你将会发现显示像下面这样:

      用你上一节中创建的管理员账号登陆,然后看一看。你会发现,没有你的blog应用。不用担心,因为你马上就要做这些了。

    08. Connecting your blog app

      在你的blog目录里面,创建一个一个admin.py文件,然后在里面写上下面代码:

    from django.contrib import admin
    from blog.models import Post
    
    admin.site.register(Post)

      这是添加应用到admin最简单的方法。如果,你重启的服务器运行一下命令syncdb然后刷新一下admin的页面,你将会看到像下面的页面:

      现在你的blog应用已经在你的admin中了,现在创建一个blog post,记着你的post的内容是HTML格式的。花一点时间做这个,因为下一部分的教程中将会用到一些posts。

      为了将你的blog应用添加到admin,你刚才做的是最基本的。为了让admin更加用户友好一点,添加一个用户自定义类PostAdmin。修改admin.py文件,让它像下面这样:

    from django.contrib import admin
    from blog.models import Post
    
    class PostAdmin(admin.ModelAdmin):
        list_display = ['title','description']
        list_filter = ['published','created']
        search_fields = ['title','description','content']
        date_hierarchy = 'created'
        save_on_top = True
        prepopulated_fields = {"slug":("title",)}
    
    
    
    admin.site.register(Post,PostAdmin)

      回到浏览器,刷新页面。你将会看到你的blog应用会好多了。

      关于这一方面,如果你想进一步研究的话,请查看 官方网站 https://docs.djangoproject.com/en/dev/ref/contrib/admin/

      目前为止,你已经做了:

    * 编辑 settings.INSTALLED_APPS 让其包含 django.contrib.admin

    * 编辑 netmag.urls 让其包含admin相应的urls模式

    * 经常检查服务器和浏览器,查看是否有错误

    * 运行syncdb命令去创建admin数据库表

    * 用管理员账号登陆

    * 创建一些post

    * 写自定义admin类

      所以你可以创建、更新、删除你的博客。但是现在,没有任何views,这就需要进行下一步。

    09. Writing the URLS, views and templates for the blog app

      现在是时候写一些urls,让它包含一些url模式。Django用URLconfs中的urlpatterns来将HTTP请求映射到特定的views函数,view函数返回响应给用户。

      总共有三部需要做:

    1. 在netmag/urls.py中写urlpatterns

    2. 在blog/views.py中写view函数

    3. 为views创建templates

    10. Write the urlpatterns

      打开文件netmag/urls.py,编辑它,使其像下面这样:

    from django.conf.urls import patterns, include, url
    
    # Uncomment the next two lines to enable the admin:
    from django.contrib import admin
    admin.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/',include(admin.site.urls)),
        url(r'^$', 'blog.views.index'),
        url(r'^(?P<slug>[w-]+)/$','blog.views.post'),
        # Examples:
        # url(r'^$', 'netmag.views.home', name='home'),
        # url(r'^netmag/', include('netmag.foo.urls')),
    
        # Uncomment the admin/doc line below to enable admin documentation:
        # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        # url(r'^admin/', include(admin.site.urls)),
    )

      如果你对^(?P<slug>[w-]+)/$比较好奇,请看 https://docs.djangoproject.com/en/dev/ref/contrib/admin/。如果看不懂,没关系,正则表达式的知识在django中一直有涉及,你可以慢慢学习。

      正则表达式^admin/在^(?P<slug>[w-]+)/$前面很重要。因为在正则表达式的世界里,后者同样匹配前者匹配的。这样,本应该映射admin的url结果映射到blog.views.post函数里,而且这不是想要的。

      如果你现在在浏览器输入http://127.0.0.1:8000/, 你将会看到一个错误提示信息ViewDoesNotExist. 你收到这个错误提示,是因为你映射到的view函数不存在,现在让我们完善这一点。

    11. Write the view functions

      打开文件blog/views.py, 修改它让其像下面这样:

    from django.shortcuts import render,get_object_or_404
    from blog.models import Post
    
    def index(request):
        posts = Post.objects.filter(published=True)
        return render(request,'blog/index.html',{'posts': posts})
    
    def post(request, slug):
        post = get_object_or_404(Post,slug=slug)
        return render(request,'blog/post.html',{'post': post})

      现在你刷新一下页面,仍然会有一个错误,但是这次是TemplateDoesNotExist错误。你接到这个错误是因为你view函数里面提及的template不存在。

      

  • 相关阅读:
    在线捉鬼游戏开发之二
    在线捉鬼游戏开发之二
    在线捉鬼游戏开发之一
    Oxygen-Dapr.EshopSample 部署随记
    word2010 标题自动编号设置
    饿了么element 全屏加载中
    vue笔记
    那些骂鸿蒙的人,我想说……
    为什么我们要在 Sketch 中备份所有 Figma 设计
    好的设计要多分享,5款优秀在线原型设计案例
  • 原文地址:https://www.cnblogs.com/Happy-Tuesday/p/3353107.html
Copyright © 2020-2023  润新知