在Python / Django中存储UI消息字符串的最佳实践是什么?

时间:2022-02-16 20:07:25

I'm wondering what the "best practice" is for storing medium length strings to be used in a UI in Python/Django.

我想知道在Python / Django中存储用于UI的中长字符串的“最佳实践”是什么。

Example: I have an error.html template that takes an error_description field. This is a few sentences explaining to the user what went wrong, and what they might do to address it. It may be different for different error pages, but remains fairly stable in the code(there's no reason that someone who can't push source code should be able to modify it), and can easily be held in memory, so I don't think it's the sort of thing that should be kept in the database.

示例:我有一个带有error_description字段的error.html模板。这是一些句子向用户解释出了什么问题,以及他们可能采取什么措施来解决这个问题。对于不同的错误页面可能会有所不同,但在代码中保持相当稳定(没有理由不能推送源代码的人应该能够修改它),并且可以很容易地保存在内存中,所以我不会认为这是应该保存在数据库中的那种东西。

My current idea is that I should just create some kind of messages.py file that has a bunch of string constants like this:

我当前的想法是,我应该创建一些messages.py文件,其中包含一堆字符串常量,如下所示:

ERROR_UNAUTHENTICATED_AJAX_EXPLANATION = "Hello, so the only way that this error should occur is if someone attempts to directly call our AJAX endpoint without having the verification code. Please don't do this, it's against the principles of this art projects."

In general, is there some canonical way to store strings that are "too flexible to be hard coded in", but "too small and static for databases"(and don't scale with your usage)? I'm thinking of the sort of thing that would be in a strings.xml file in an Android project.

一般来说,是否有一些规范的方法来存储“太灵活而不能硬编码”的字符串,但“对于数据库来说太小而且静态”(并且不能随着您的使用而扩展)?我正在考虑Android项目中的strings.xml文件中会出现的那种情况。

Other possibilities I'm juggling include a text file that views.py reads and stores as constants, actually just hardcoding them, and sticking them in template files.

我正在玩杂耍的其他可能性包括一个text.py读取和存储为常量的文本文件,实际上只是对它们进行硬编码,并将它们粘贴在模板文件中。

There's a lot of ways to do this, and it's not a very complicated thing, I just want to know which one is the most 'right'.

有很多方法可以做到这一点,这不是一件非常复杂的事情,我只想知道哪一个是最“正确”的。

Thanks! And let me know if you need more info!

谢谢!如果您需要更多信息,请告诉我们!

1 个解决方案

#1


6  

If you are absolutely sure, these strings never need to become dynamic just create a strings.py module and drop the strings there as variables ("constants")

如果您完全确定,这些字符串永远不需要变为动态,只需创建一个strings.py模块并将字符串作为变量删除(“常量”)

However, as the messages are user visible, you will most likely need to localize them at some point in your application's lifetime. Consequently, please make use of Django's excellent gettext support:

但是,由于消息是用户可见的,因此您很可能需要在应用程序生命周期的某个时刻对它们进行本地化。因此,请使用Django优秀的gettext支持:

https://docs.djangoproject.com/en/1.7/topics/i18n/

#1


6  

If you are absolutely sure, these strings never need to become dynamic just create a strings.py module and drop the strings there as variables ("constants")

如果您完全确定,这些字符串永远不需要变为动态,只需创建一个strings.py模块并将字符串作为变量删除(“常量”)

However, as the messages are user visible, you will most likely need to localize them at some point in your application's lifetime. Consequently, please make use of Django's excellent gettext support:

但是,由于消息是用户可见的,因此您很可能需要在应用程序生命周期的某个时刻对它们进行本地化。因此,请使用Django优秀的gettext支持:

https://docs.djangoproject.com/en/1.7/topics/i18n/