Py学习  »  问与答

[精华] Django模板中引入了JS变量,请问如何比较两者的大小

TMAC狂热-weibo • 8 年前 • 7960 次点击  

在Django中引入了JQcloud标签云插件,需要比较JS变量与Django模板变量的关系 求解~

<script type="text/javascript">
  /*!
   * Create an array of word objects, each representing a word in the cloud
   */

     //var list=[["Java", 8], ["Android", 0], ["数据库", 1], ["算法", 0]];
     var list = {{ tagCloud|safe}};


     var word_array = new Array(); 
     for( i in list )
      {
        var temp = list[i][0];

        {% for j in tags %}
         //tags = ["Java","Android","数据库","算法"]

            {% if j == temp %}
            //比较JS变量与Django模板变量的大小  这种方法表示不可行
            //求解决

             word_array.push({text:list[i][0],weight:list[i][1],link:{ href: "{% url 'tagDetail' j %}"}});

            {% endif %}

        {% endfor %}


            //word_array.push({text:list[i][0],weight:list[i][1],link: "http://www.tmackan.com/articleTag/" + temp});


      }


   /*var word_array = [
      {text: "Lorem", weight: 15},
      {text: "Ipsum", weight: 9, link: "http://jquery.com/"},
      {text: "Dolor", weight: 6, html: {title: "I can haz any html attribute"}},
      {text: "Sit", weight: 7},
      {text: "日了", weight: 15}
      // ...as many words as you want
  ];*/

  $(function() {
    // When DOM is ready, select the container element and call the jQCloud method, passing the array of words as the first argument.
    $("#example").jQCloud(word_array);
  });
</script>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1136
 
7960 次点击  
文章 [ 14 ]  |  最新文章 8 年前
TMAC狂热-weibo
Reply   •   1 楼
TMAC狂热-weibo    8 年前

解决了= =

for(i in list) {
var temp = list[i][0]; 
for(var j = 0;j < tag.length;j++)
{
     if(tag[j] == temp)
   word_array.push({text:list[i][0],weight:list[i] [1],link:"/articleTag /"+temp});

}

}

解决

Py站长
Reply   •   2 楼
Py站长    8 年前

我一般的做法是 将 django的模板数据放在html代码里,比如你可以搞一个隐藏的字段

<input type="hidden"/> 把数据扔里面

然后在 js文件去 $("#").val() 去拿数据进行操作,进而与JS代码进行比较,

这样可以做到 js代码与 django代码分离解耦,适合开发和扩展、调试。

TMAC狂热-weibo
Reply   •   3 楼
TMAC狂热-weibo    8 年前

@Django中国社区

mugbya
Reply   •   4 楼
mugbya    8 年前

~ 才玩django不久~ 也是小白

TMAC狂热-weibo
Reply   •   5 楼
TMAC狂热-weibo    8 年前

一直报这个错误 - -

NoReverseMatch at /

Reverse for 'tagDetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['articleTag/(?P<tag>\\w+)/$']
TMAC狂热-weibo
Reply   •   6 楼
TMAC狂热-weibo    8 年前

@mugbya 见谅,小白按照别人的教材来做一个简单的个人博客,卡在标签云这了.

TMAC狂热-weibo
Reply   •   7 楼
TMAC狂热-weibo    8 年前

view.py里面写错了,纠正一下

def home(request):
  tags = json.dumps(Tag.tag_list.get_Tags(),ensure_ascii=False)
  return render_to_response('blog/index.html',
        locals(), #它返回的字典对所有局部变量的名称与值进行映射。
        context_instance=RequestContext(request))
TMAC狂热-weibo
Reply   •   8 楼
TMAC狂热-weibo    8 年前

@mugbya

view.py

def home(request):
   tags = json.dumps(Tag.tag_list.get_Tags())

   return render_to_response('blog/index.html',
        locals(), #它返回的字典对所有局部变量的名称与值进行映射。
        context_instance=RequestContext(request))

Template页面

<script type="text/javascript">
  /*!
   * Create an array of word objects, each representing a word in the cloud
   */

     //var list=[["Java", 8], ["Android", 0], ["数据库", 1], ["算法", 0]];
     var list = {{ tagCloud|safe}};

     var word_array = new Array();

 var tag = {{ tags|safe }};
     //tag = ["Java","Android","数据库","算法"]

   for(i in list)
{

   var temp = list[i][0]; 
   for(var j = 0;j < tag.length;j++)
  {

        if(tag[j] == temp)

        word_array.push({text:list[i][0],weight:list[i][1],link:"{%  url 'tagDetail' tags.j %}"});

    }

 }           
  //word_array.push({text:list[i][0],weight:list[i][1],link:    "http://www.tmackan.com/articleTag/" + temp});     
   /*var word_array = [
      {text: "Lorem", weight: 15},
      {text: "Ipsum", weight: 9, link: "http://jquery.com/"},
      {text: "Dolor", weight: 6, html: {title: "I can haz any html attribute"}},
      {text: "Sit", weight: 7},
      {text: "日了", weight: 15}
      // ...as many words as you want
  ];*/

  $(function() {
    // When DOM is ready, select the container element and call the jQCloud method, passing the array of words as the first argument.
    $("#example").jQCloud(word_array);
  });

</script>

urls.py

url(r'^articleTag/(?P<tag>\w+)/$', 'article.views.tagDetail',  name="tagDetail"),#每个标签页下面的文章
mugbya
Reply   •   9 楼
mugbya    8 年前

@TMAC狂热-weibo 额,var tag ={{tags}}; 这句话你都有调试跟踪?

TMAC狂热-weibo
Reply   •   10 楼
TMAC狂热-weibo    8 年前

@mugbya 可以的,我从views.py里面传过来的

mugbya
Reply   •   11 楼
mugbya    8 年前

没这么做过....你的 tags 能获得所有的值么?

TMAC狂热-weibo
Reply   •   12 楼
TMAC狂热-weibo    8 年前

TemplateSyntaxError at /

Could not parse the remainder: '[j]' from 'tag[j]'

var temp = list[i][0];

        //此处url跳转采用的是硬编码= =


        var tag ={{tags}};
        for(var j = 0;j < tag.length;j++)
{

            if(tag[j] == temp)

            word_array.push({text:list[i][0],weight:list[i][1],link:{ href: "{% url 'tagDetail' tag[j] %}"}});

    }
MCC
Reply   •   13 楼
MCC    8 年前
var temp = list[i][0];
var tags = {{ tags }};
for (var i=0;i<tags.length;i++){
    if (tags[i] == temp){
     //blablabla....
    }
}

这个样做不行么

TMAC狂热-weibo
Reply   •   14 楼
TMAC狂热-weibo    8 年前

@mugbya