python - django的restful部分给客户端提供接口,我在代码里写中文,有问题吗?
黄舟
黄舟 2017-04-18 09:16:16
0
3
674
def share_to_user_api(request, repo, to_user, permission):
    """Share repo to a user with given permission.
    """
    repo_id = repo.id
    from_user = request.user.username

    if from_user == to_user:
        msg = ('%s 共享失败,不能共享给本用户')% to_user

        return msg

    if not is_registered_user(to_user):
        msg = ('%s 共享失败,该用户未注册') % to_user

        return msg

    try:
        if seafile_api.share_repo(repo_id, from_user, to_user, permission) == 2:
            msg = ('%s 共享失败,已共享') % to_user

            return msg
        else:
            seafile_api.share_repo(repo_id, from_user, to_user, permission)
    except SearpcError as e:
            logger.error(e)
            msg = ('%s 共享失败,请稍后重试') % to_user

            return msg
    else:
        # send a signal when sharing repo successful
        share_repo_to_user_successful.send(sender=None,
                                           from_user=from_user,
                                           to_user=to_user, repo=repo)
        msg = ('%s 共享成功') % to_user

        return msg

因为返回给客户端的信息直接提示给用户,综合考虑,提示由服务器返回比较合适,我这样写中文有其他的可能的问题出现吗?

qt客户端适当的转换编码格式,就是中文了,我怕后期有什么不确定的因素,导致客户端原来的编码格式显示出来的是乱码。目前测试是没问题。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
大家讲道理

This is not good. Returning a dict is more appropriate. {status: xxx, msg: xxx, data:xxx}, 成功调用设置http status = 200, 其它根据情况返回40x, 50x.

In addition, when it comes to restinterfaces, it is recommended to learn Django rest framework. There is a certain cost to learn, but it is very useful once you learn it.

Peter_Zhu

If you write Chinese directly in the Python source code, Python will convert the encoding to ANSI encoding. Under the Simplified Chinese system, the ANSI code is GB2312. If the web page is UTF8 encoded, it may be garbled. It is recommended to use unicode encoding.


a = u'你好'
阿神

Writing like this is very likely that the client will receive garbled characters.
You can change it like this. Statistics use unicode.
It’s no problem to use python3 to write Chinese in the code.
Use python 2.7+. It’s best to write the file header.

 #! -*- coding:utf-8 -*-
from __future__ import unicode_literals

This way the file doesn’t need to be modified,

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template