古城童话
  • Search results
古城童话
July 31st, 2016

kramdown 使用技巧

kramdown 已经成为 github pages 的 jekyll 静态网站生成器目前唯一支持的 markdown 引擎,参见 Updating your Markdown processor to kramdown

下面是一些使用karmdown 的小技巧,仅供参考。

Read more
June 25th, 2016

获取自己的公网IP

如何能够获取自己的公网IP,下面是一些方法:

  1. 使用一些网站提供的服务,这样的网站有很多,比如下面这些:
  2. 使用openDNS提供的服务

    dig +short myip.opendns.com @resolver1.opendns.com
    
Read more
June 21st, 2016

python中下划线的作用

下划线在python中有3中常见用法:

  1. 在一个python解释器的交互界面中获取上一个执行语句的结果。这个最先是由标准的CPython解释器所设定的,也被其他的python解释器所效仿。
  2. 用于i18n的翻译查找(从相关的C语言惯例中引入)。比如代码raise forms.ValidationError(_("Please enter a correct username"))
  3. 一般用于在标记在一个函数的返回结果中故意被忽略的无用变量。比如在代码label, has_label, _ = text.partition(':')中返回值中的第三个变量被故意忽略。

后两种用法会导致冲突,所以在代码中使用了 _ 来指示i18n翻译时,有必要避免使用 _ 来标记一个故意被抛弃的变量。因为这个原因,很多人喜欢使用双下划线 __ 来表示故意被抛弃的变量。

参考资料: What is the purpose of the single underscore variable in Python?

Read more
May 13th, 2016

django博客存档的实现

最近我正在学习Django写一个个人博客,其中要实现一个按照年和月生成博客归档的功能。查询资料后实现如下: 在blog.models中定义博客模型如下:

class Post(models.Model):
    title = models.CharField(max_length=128)
    body = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey('auth.User')
    tags = models.ManyToManyField(Tag)
    
    def __str__(self):
        return self.title
Read more