使用simplejson模块将用户名转储为数组

时间:2013-02-05 00:03:48

标签: python django twitter-bootstrap simplejson bootstrap-typeahead

我正在尝试在django中实现bootstrap typeahead。在我的模板文件中,我想要一个文本框供用户输入文本[在我的情况下是用户名]。我正在拍摄的方法是让django向user_json提供数组show.html,因此bootstrap typeahead可以使用该数组。

在我的views.py文件中,我添加了以下功能:

def users_list_json(request):
    users = User.get_username()

    user_json = simplejson.dumps(users)
    render_to_response("show.html", {"user_json": user_json})

但是当我在show.html中运行console.log(user_json)时会抛出一个错误(ReferenceError:user_json未定义)。我应该怎么做呢?有没有更简单的方法?

我想要一个包含所有用户名的数组,以便在show.html文件中使用,因此我可以将其用于typeahead。

1 个答案:

答案 0 :(得分:0)

我通过改变功能解决了这个问题。

def users_list_json(request):
    user_list = [u.username for u in User.objects.exclude(username=username)]
    context.user_json_list = simplejson.dumps(user_list)

然后,我通过添加:

在show.html中调用了数组

{{ user_json_list|safe }}