如何发送Json或“dict”使用Django消息框架 [英] How to send Json or "dict" using Django Messages Framework

查看:175
本文介绍了如何发送Json或“dict”使用Django消息框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django Messaging 框架发送有关验证错误的其他数据:

  def clean_X(self):
xuser_id = self.cleaned_data ['xuser_id']
如果xuser.objects.filter(xuser_id = xuser_id).exists():
available_now = {available :[example,hello,ahc,sdcsd]}
messages.error(self.request,message = available_now,extra_tags =available_ids)
raise forms.ValidationError '抱歉!用户名%(xuser_id)s已经被采取,请尝试另一个或从以下选择一个:',params = {xuser_id:xuser_id}
return xuser_id

当尝试在模板中访问时,消息将转换为字符串:如

 {available:[example,hello,ahc,sdcsd]}
pre>

使编程难以访问,即 message.available



如何我可以使用 Django-Messages 直接发送一个json到模板。我的意图在这里,不只是显示一个消息,而是使可用的id 的可点击(像gmail自动建议的用户名)



谢谢!

解决方案

  {%消息%} 
< p> {{message.tags}}< / p>
{%if message.tags =='available_ids error'%}
{%for obj,message.message.items%中的值}
< div>
{{obj}}
{%for values%}
< p class =user-id> {{val}}< / p>
{%endfor%}
< / div>
{%endfor%}
{%endif%}
{%endfor%}

您可以使用模板中的上述代码片段,使其正常工作,并根据您的要求进行修改。



工作原理



我们遍历每个消息,因为我们在消息中插入字典,所以我们必须迭代字典和里面的字典我们有列表,所以我们也必须迭代。
因此,我们必须使用三个for循环。
你必须应用一些条件,如你必​​须迭代?您可以使用标签查看。



这里我有硬编码(迭代条件)。



编辑:



使用两个for循环
用这些行更新您的clean_X

  available_now = [example,hello,ahc,sdcsd] 
messages.error(self.request,message = available_now,extra_tags =available_ids)

并在模板中使用这些行

 code> {%message in message%} 
< p> {{message.tags}}< / p>
{%if message.tags =='available_ids error'%}
< div>
{message for message.message%中的val%
< p class =user-id> {{val}}< / p>
{%endfor%}
< / div>
{%endif%}
{%endfor%}


I'm using Django Messaging framework to send additional data on the validation error:

def clean_X(self):
        xuser_id = self.cleaned_data['xuser_id']
        if xuser.objects.filter(xuser_id=xuser_id).exists():
            available_now = {"available" : ["example","hello","ahc","sdcsd"]}
            messages.error(self.request, message = available_now,extra_tags="available_ids")
            raise forms.ValidationError('Sorry! User ID "%(xuser_id)s" is already taken, Please try another or chose one from following:', params={"xuser_id" : xuser_id})
        return xuser_id 

The message is converted as a string when tried to access in the template: like

"{"available" : ["example","hello","ahc","sdcsd"]}"

making difficult to access programmatically i.e message.available

How can i send a json directly to the template using Django-Messages. My intention is here, not just to display a message rather make available-id's clickable (like the one in gmail auto-suggestion of username)

Thanks!

解决方案

{% for message in messages %}
    <p>{{ message.tags }}</p>
    {% if message.tags == 'available_ids error' %}
    {% for obj,values in message.message.items %}
        <div>
            {{ obj  }} 
            {% for val in values %}
                <p class="user-id">{{ val }}</p>
            {% endfor %}
        </div>
    {% endfor %}
    {% endif %}
{% endfor %}

You can use above code snippet inside template to make it working and modify it according to your requirements.

How it works?

We iterate through each and every message, as we have inserted dictionary in messages so we have to iterate dictionary and inside dictionary we have list so we have to iterate that too. Therefore we have to use three for loop. You have to apply some conditions like when you have to iterate? You can check using this by tags.

Here I have hard coded (iterating condition) for this purpose.

Edit:

Using two for loops Update your clean_X with these lines

available_now = ["example","hello","ahc","sdcsd"]
messages.error(self.request, message = available_now,extra_tags="available_ids")

and use these lines in templates

{% for message in messages %}
    <p>{{ message.tags }}</p>
    {% if message.tags == 'available_ids error' %}
        <div>
            {% for val in message.message %}
                <p class="user-id">{{ val }}</p>
            {% endfor %}
        </div>
    {% endif %}
{% endfor %}

这篇关于如何发送Json或“dict”使用Django消息框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆