-
從模板上來說
-
前端模板上的使用的語法有些區別
-
tornado可以通過render傳類及函數
class Calculation: def sum(self,a,b): return a+b class UiHandler(tornado.web.RequestHandler): def func(self): return 'arrow' def get(self): name = self.get_argument('name', 'no') self.render( 'index.html', name = name, func=self.func, cal=Calculation, )
-
tornado可以通過ui_methods,ui_modules傳函數和類
handlers = [ui_methods=util.uimethod, ui_modules=util.uimodules, ]
-
ui_methods中的函數要傳self參數
def methods1(self): return 'ui_methods1'
-
ui_modules要繼承UIModule類
from tornado.web import UIModule class Advertisdement(UIModule): def render(self, *args, **kwargs): return self.render_string('07ad.html') #render傳html文件 def css_files(self): return "/static/css/King_Chance_Layer7.css" #css_files傳css文件 def javascript_files(self): #javascript_files傳js文件 return [ "/static/js/jquery_1_7.js", "/static/js/King_Chance_Layer.js", "/static/js/King_layer_test.js", ]
-
-
django可以通過上下文渲染器來傳
-
創建上下文渲染器文件context_processors.py
from .models import GoodsCategory def category_list(request): category_list = GoodsCategory.objects.filter(status=0).all() return {"category_list": category_list}
-
添加到settings的TEMPLATES中
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.static', 'shop.context_processors.category_list', ], }, }, ]
-
在前端頁面使用
<div class="breadcrumb"> <a href="{% url 'shop:index' %}">全部分類</a> <span>></span> <a href="#">{{ goods_category.name }}</a> </div> <div class="main_wrap clearfix"> <div class="l_wrap fl clearfix"> <div class="new_goods"> <h3>新品推薦</h3> {% refferral_goods cid %} </div> </div>
-
-
django可以通過自定義標簽來傳
-
在app下創建templatetags包在該目錄下創建tags.py
@register.simple_tag def divide_page(curr_page, page_obj, url_name, request_url, page_name="", args=(), kwargs={}): """ 算法 1、先獲取所有頁碼列表 range_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] 2、 然后定義需要展示的數目,這里定義為一個5 max_page_count = 5 3、獲取中間位置前后需要加減索引 center_index = max_page_count / 2 4、獲取當前頁面索引,這里定義為當前頁面為5 curr_index = range_list.index(5) range_list[curr_index-center_index:] [3, 4, 5, 6, 7, 8, 9] 5、循環處理右邊 獲取一個計數器 當計數器的索引大於總數目時退出 [3, 4, 5, 6, 7, 8, 9] 迭代這個時 當迭代到7的位置,計數器的值就為6,退出循環,就獲取到以下列表 [3, 4, 5, 6, 7] """ def parse_qs(qs): res = {} params = qs.split("&") for p_str in params: k, v = p_str.split("=") res[k] = urllib.unquote(v) return res url = reverse(url_name, args=args, kwargs=kwargs) # 點擊頁碼需要跳轉的url前綴 # 默認為unicode,這里修改為utf8 url = url.encode("utf8") page_str = '<div class="pagenation">' max_page_count = 5 page = page_obj.page(curr_page) # 獲取當前get參數 params = parse_qs(urlparse.urlparse(request_url).query.encode("utf8")) if not page_name: page_name = "curr_page" # 生成上一頁html if page.has_previous(): params[page_name] = curr_page - 1 curr_url = "%s?%s" % (url, urllib.urlencode(params)) page_str += '<a href="%s" style=" 上一頁 </a>' % curr_url center_index = max_page_count / 2 page_range = [c for c in page_obj.page_range] page_index = page_range.index(curr_page) if page_index >= center_index: page_range = page_range[page_index-center_index:] i = 1 # 生成中間頁碼html for cp in page_range: params[page_name] = cp # curr_url = "%s?%s" % (url, "&".join(["%s=%s" % (k,v) for k,v in params.items()])) curr_url = "%s?%s" % (url, urllib.urlencode(params)) if cp == curr_page: page_str += '<a href="%s" class="active" style="%s</a>' % ( curr_url, cp) else: page_str += '<a href="%s" >%s</a>' % ( curr_url, cp) i += 1 if i > max_page_count: break # 生成下一頁html if page.has_next(): params[page_name] = curr_page + 1 curr_url = "%s?%s" % (url, urllib.urlencode(params)) page_str += '<a href="%s" style=" 下一頁 </a>' % curr_url page_str += "</div>" return mark_safe(page_str)
-
在前端模板中調用
-
{% extends 'shop_base.html' %} {% load tags %} {% block shop_js %} <script type="text/javascript" src="{{ STATIC_URL }}js/list.js"></script> {% endblock shop_js %} {% block index_content %} {{ block.super }} <div> {% divide_page curr_page p 'shop:categorys' request.get_full_path kwargs=params %} </div>
-
-
-
從數據庫來說
django有自己的ORM,而tornado的torndb不是很強大,所以一般都使用sqlalchemy
-
從視圖上來說
-
django可以用form做一些驗證
-
render中django沒有tornado可以傳的參數類型多
-
-
性能上來說
tornado由於是單線程異步回調的模式,所以比django的並發要高
django是多線程但是沒有做異步,所以要比tornado的並發低
-
從提供的插件上來說
django提供了ORM,django.core.mail,django crontab,等等