更改返回的输出在 Django 中的显示方式
我最近开始整体学习 Python/Django,为了加快我的学习曲线,同时做一些建设性的事情,我开始了自己的个人项目。
我已经安装了最新的 Django/Python/Jinja2 和 Python Battle.net API Egg。 目前我正在查询一个“字符”,并且我正在尝试更改返回值的输出,这是我的视图中的函数:
def viewCharacter(request, name):
character = get_object_or_404(Member, name=name)
info = Character('EU', 'Auchindoun', name, fields=[Character.GUILD])
ctx = { 'character': character, 'info': info, 'guildname': 'Guild Name' }
return render_to_response("roster/viewCharacter.html", ctx, request)
现在,在我的模板中,我尝试了“翻译” info.class_ (它返回一个数值)从它的数值到一个字符串(类名),但我总是收到有关 info.class_ 无法在 if/for 语句/循环或其他错误中使用的错误消息。 (尝试将其与二元组进行比较)
我真的找不到在线执行此操作的方法,因此我来到了在学习过程中对我帮助最大的一个地方。
任何帮助将不胜感激! - 尼埃鲁
I recently started learning Python/Django as a whole and in an attempt to speed up my learning curve and at the same time do something constructive I've started my own personal project.
I've got the latest Django/Python/Jinja2 installed together with the Python Battle.net API egg.
Currently I'm querying for a "character" and I'm trying to change the output of a returned value, here's the function from my views:
def viewCharacter(request, name):
character = get_object_or_404(Member, name=name)
info = Character('EU', 'Auchindoun', name, fields=[Character.GUILD])
ctx = { 'character': character, 'info': info, 'guildname': 'Guild Name' }
return render_to_response("roster/viewCharacter.html", ctx, request)
Now, in my template, I've tried "translating" info.class_ (which returns a numeric value) from it's numeric value to a string (The class name) but I'm always getting error messages about info.class_ not being able to be used in if/for statements/loops or other errors. (Tried comparing it to a two-tuple)
I really can't find a way to do this online, so I've come to the one place that have helped me the most in my learning process.
Any help would be most appreciated!
- Nieeru
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有什么原因不能像这样向上下文添加另一个变量:
ctx = { 'character': character, 'info': info, 'class': repr(info.class_), 'guildname': 'Guild Name ' }
编辑:根据您提供的附加信息,这是我的新建议。
更改
为:
这只是类在视图中查找。现在使用
{{ className }}将其添加到您的模板中
Is there any reason you can't add another variable to the context like so:
ctx = { 'character': character, 'info': info, 'class': repr(info.class_), 'guildname': 'Guild Name' }
EDIT: With the additional information you provided, here is my new suggestion.
Change:
to:
This simply does the class look up in the view. Now add it to your template using
{{ className }}
如果您确实需要在模板中使用类名,请尝试使用 this 模板过滤器,或者只是将其放入视图中并传递上下文:)
If you really need to use a classname in template, try using this template filter, or just get it in the view and pass in a context :)