zoukankan      html  css  js  c++  java
  • django 快速搭建blog(三)

    http://www.cnblogs.com/fnng/p/3737964.html

    引用自此博客


     

     

    创建blog的公共部分

    从Django的角度看,

    一个页面

    具有三个典型的组件:

    一个模板(template):模板负责把传递进来的信息显示出来。

    一个视图(view):视图负责从数据库获取需要显示的信息。

    一个URL模式:它负责把收到的请求和你的试图函数匹配,有时候也会向视图传递一些参数。

     1.模板

    2.视图

    3,创建一个URL模式

    在mysite/urls.py,加上:url(r'^blog/', include('blog.urls')),

    在blog/urls.py,加上: from django.conf.urls import * from blog.views import archive urlpatterns = patterns('',                       url(r'^$',archive),                       )

      有报错类:1:from django.conf.urls import *找不到patterns模块? Django-1.10,python27

      解决:这个特性在1.9就声明了deprecated. 1.10正式移除了。使用 django 1.10 需要改用 django.conf.urls.url() 示范代码:

      意思就是改一下代码的格式

    from django.conf.urls import url
     
    from . import views
     
    urlpatterns = [
        url(r'^articles/2003/$', views.special_case_2003),
        url(r'^articles/([0-9]{4})/$', views.year_archive),
        url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
        url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
    ]

    遇到问题2:

    TypeError at /blog/
    context must be a dict rather than Context.
    Request Method:    GET
    Request URL:    http://127.0.0.1:8000/blog/
    Django Version:    1.11
    Exception Type:    TypeError
    Exception Value:    
    context must be a dict rather than Context.
    Exception Location:    C:Python27libsite-packagesdjango	emplatecontext.py in make_context, line 287
    Python Executable:    C:Python27python.exe
    Python Version:    2.7.12
    Python Path:    
    ['C:\Users\yangyang5\mysite',
     'C:\Windows\system32\python27.zip',
     'C:\Python27\DLLs',
     'C:\Python27\lib',
     'C:\Python27\lib\plat-win',
     'C:\Python27\lib\lib-tk',
     'C:\Python27',
     'C:\Python27\lib\site-packages',
     'C:\Python27\lib\site-packages\wx-2.8-msw-unicode']
    Server time:    Mon, 10 Apr 2017 06:28:23 +0000

    貌似是说views.py在调用get_temllate时有问题

    解决办法:换了种写的方式

    from django.shortcuts import render
    from django.shortcuts import render_to_response
    from django.template import loader,Context
    from django.http import HttpResponse
    from blog.models import BlogPost
    
    # Create your views here.
    
    def archive(request):
        blog_list=BlogPost.objects.all()    
        return render_to_response('archive.html',{'blog_list':blog_list})

    blog_list = BlogPost.objects.all() :获取数据库里面所拥有BlogPost对象

    render_to_response()返回一个页面(index.html),顺带把数据库中查询出来的所有博客内容(blog_list)也一并返回。

    最后的润色

    创建一个base.html,别的html都可以让它引用base.html模板和它的“content”块

  • 相关阅读:
    在python中使用正则表达式(二)
    在python中使用正则表达式(一)
    利用cookies进行登录并封装请求方法
    fiddler学习笔记&&基本使用
    理解css相邻兄弟选择器
    selenium+Page Objects(第三话)
    selenium+Page Objects(第二话)
    selenium+Page Objects(第一话)
    python+selenium基础之XPATH轴定位(第二篇)
    关于类、方法、对象(实例):静态方法
  • 原文地址:https://www.cnblogs.com/mogujiang/p/6687656.html
Copyright © 2011-2022 走看看