[django] Variables and attributes may not begin with underscores

当尝试在模板中调用以下划线开头的对象时,会报如下类似错误:

TemplateSyntaxError at /

Variables and attributes may not begin with underscores: 'user._meta.get_field('name').help_text'

解决方法就是,将调用以下划线开头的对象的操作封装到模板过滤器中。

如何创建自定义模板过滤器

在 models.py 文件所在目录新建一个 templatetags 目录:

hello/
    models.py
    templatetags/
        hello_extras.py
        __init__.py
    views.py

hello_extras.py 中保存着我们自定义的模板过滤器。

在 hello_extras.py 文件的开头需要包含如下代码:

from django import template

register = template ...
more ...


[linux]入手 VPS 后首先该做的事情

本文将介绍入手 VPS 后首先该做的一些事情。

本文推荐使用 Xshell 作为 Windows 下的 SSH 客户端。

更改 root 用户密码

使用 root 登录 ssh 后,

Xshell:\> ssh root@192.168.1.2

首先要做的事情就是更改 root 密码,密码记得要复杂点:

# passwd
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

修改完密码之后可以断开 ssh 重新使用 root 登录验证一下密码。

创建普通用户

为了安全,平时我们应该以普通用户的身份操作 ...

more ...