简体   繁体   English

遍历字符串和列表的Python / Django模板

[英]Python/Django Template Iterating through Strings and Lists

I have an issue with Python/Django where I have either a String or a List of Strings being passed to a Template. 我在Python / Django中遇到问题,我将一个字符串或一个字符串列表传递给模板。 If it is a list I need to iterate through it and output each String on a separate line, and if it is a String I just need to output the string. 如果它是一个列表,我需要遍历它,并在单独的一行上输出每个字符串,如果它是一个字符串,我只需要输出该字符串。 I need to be able to differentiate between the value types and adjust accordingly. 我需要能够区分值类型并进行相应调整。

Currently I have code similiar to this: 目前,我有与此类似的代码:

if isinstance(values, list):
    for value in values:
        html += value + "<br />"
else:
    html += values + "<br />"

My problem is twofold: 我的问题是双重的:

Firstly, I am looking for a better/more pythonic way (if possible) to do this which will have the same result. 首先,我正在寻找一种更好/更多的pythonic方式(如果可能的话)来做到这一点,它将获得相同的结果。 I know that for some situations the isinstance method is not an ideal solution, but would I be able to utilize something like hasattr and would it be an improvement in terms of efficiency? 我知道在某些情况下, isinstance方法不是理想的解决方案,但是我可以利用hasattr东西吗?这会在效率方面有所改善吗?

Secondly, I would ideally want this to be implemented using the Django Template language. 其次,理想情况下,我希望使用Django模板语言来实现。 If I continue using isinstance or change to hasattr I would have to either make a custom Template filter or tag to be able to pass the proper arguments. 如果我继续使用isinstance或更改为hasattr ,则必须制作一个自定义模板过滤器或标签,以便能够传递适当的参数。 Should I forget the template and just write a the code that generates HTML in the view (bad practice) or would the answers to one of these be the best approach for my situation? 我应该忘了模板,只写一个在视图中生成HTML的代码(不好的做法),或者对这些问题之一的回答是我所处情况的最佳方法吗? ( Performing a getattr() style lookup in a django template or django template system, calling a function inside a model ) 在django模板django模板系统中 执行getattr()样式查找,在 模型内调用函数

The current template code can be found here: http://pastebin.com/JK2PRrWv 当前模板代码可以在这里找到: http : //pastebin.com/JK2PRrWv

Background: 背景:

I am currently working on some Python(Django) code that implements a simple REST/Json API used to handle queries. 我目前正在研究一些Python(Django)代码,这些代码实现了用于处理查询的简单REST / Json API。 One of the requirements is to convert a list of Python dictionaries(parsed from JSON) into very simple HTML tables. 要求之一是将Python字典列表(从JSON解析)转换成非常简单的HTML表。 To implement this functionality, I have utilized a Django template that takes the list of Python dictionaries and generates HTML from it. 为了实现此功能,我利用了Django模板,该模板获取Python词典列表并从中生成HTML。

Any help/constructive criticism would be greatly appreciated. 任何帮助/建设性的批评将不胜感激。

isinstance is probably what you want to distinguish between a string and another iterable, but you should compare with basestring (from which all strings are derived) rather than list. isinstance可能是您想要区分一个字符串和另一个可迭代的对象的方法,但是您应该与basestring(从中派生所有字符串)进行比较,而不是与list进行比较。 See this question and answer. 请参阅问题和答案。

if isinstance(values, basestring):
    html += values + "<br />"
else:
    for value in values:
        html += value + "<br />"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM