سؤال

I am trying to implement bootstrap typeahead in django. In my template file, I want a textbox for users to enter text [usernames in my case]. The approach I am shooting for is to have django provide an array user_json to show.html, so bootstrap typeahead could use that array.

In my views.py file I have added the following function:

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

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

But when I run console.log(user_json) in show.html it throws an error (ReferenceError: user_json is not defined). How should I go about implementing this? Is there an easier way?

I want an array with all the usernames to be available in show.html file so I could use that with typeahead.

هل كانت مفيدة؟

المحلول

I solved the problem by changing the function a bit.

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)

And, I called the array in show.html by adding:

{{ user_json_list|safe }}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top