怎么用Django中context的上下文-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

目前創(chuàng)新互聯(lián)已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、漢中網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

本篇文章給大家分享的是有關(guān)怎么用Django中context的上下文,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

很多時候設(shè)置一個模板變量而非返回值也很有用。 那樣,模板作者就只能使用模板標(biāo)簽所設(shè)置的變量。

要在上下文中設(shè)置變量,在 render() 函數(shù)的context對象上使用字典賦值。 這里是一個修改過的 CurrentTimeNode ,其中設(shè)定了一個模板變量 current_time ,并沒有返回它:

class CurrentTimeNode2(template.Node):
  def __init__(self, format_string):
    self.format_string = str(format_string)
 
  def render(self, context):
    now = datetime.datetime.now()
    context['current_time'] = now.strftime(self.format_string)
    return ''

(我們把創(chuàng)建函數(shù)do_current_time2和注冊給current_time2模板標(biāo)簽的工作留作讀者練習(xí)。)

注意 render() 返回了一個空字符串。 render() 應(yīng)當(dāng)總是返回一個字符串,所以如果模板標(biāo)簽只是要設(shè)置變量, render() 就應(yīng)該返回一個空字符串。

你應(yīng)該這樣使用這個新版本的標(biāo)簽:

{% current_time2 "%Y-%M-%d %I:%M %p" %}
<p>The time is {{ current_time }}.</p>

但是 CurrentTimeNode2 有一個問題: 變量名 current_time 是硬編碼的。 這意味著你必須確定你的模板在其它任何地方都不使用 {{ current_time }} ,因為 {% current_time2 %} 會盲目的覆蓋該變量的值。

一種更簡潔的方案是由模板標(biāo)簽來指定需要設(shè)定的變量的名稱,就像這樣:

{% get_current_time "%Y-%M-%d %I:%M %p" as my_current_time %}
<p>The current time is {{ my_current_time }}.</p>

為此,你需要重構(gòu)編譯函數(shù)和 Node 類,如下所示:

import re
 
class CurrentTimeNode3(template.Node):
  def __init__(self, format_string, var_name):
    self.format_string = str(format_string)
    self.var_name = var_name
 
  def render(self, context):
    now = datetime.datetime.now()
    context[self.var_name] = now.strftime(self.format_string)
    return ''
 
def do_current_time(parser, token):
  # This version uses a regular expression to parse tag contents.
  try:
    # Splitting by None == splitting by spaces.
    tag_name, arg = token.contents.split(None, 1)
  except ValueError:
    msg = '%r tag requires arguments' % token.contents[0]
    raise template.TemplateSyntaxError(msg)
 
  m = re.search(r'(.*?) as (\w+)', arg)
  if m:
    fmt, var_name = m.groups()
  else:
    msg = '%r tag had invalid arguments' % tag_name
    raise template.TemplateSyntaxError(msg)
 
  if not (fmt[0] == fmt[-1] and fmt[0] in ('"', "'")):
    msg = "%r tag's argument should be in quotes" % tag_name
    raise template.TemplateSyntaxError(msg)
 
  return CurrentTimeNode3(fmt[1:-1], var_name)

現(xiàn)在 do_current_time() 把格式字符串和變量名傳遞給 CurrentTimeNode3 。

以上就是怎么用Django中context的上下文,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道。

分享題目:怎么用Django中context的上下文-創(chuàng)新互聯(lián)
分享鏈接:http://muchs.cn/article34/ddscse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)網(wǎng)站內(nèi)鏈、微信小程序ChatGPT、關(guān)鍵詞優(yōu)化、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)