如何获取对象的值并在Django Views中使用它?
我需要使用自动完成搜索的结果来过滤Django中的视图。 这是我的搜索视图:
def search_view(request):
q = request.GET['term']
print q
sc = []
users = Account.objects.filter(username__icontains=q)
for user in users:
sc.append(account.username)
content_type = 'application/json"
return HttpResponse(json.dumps(new), content_type)
在模板中,例如:用户1。我想把这个值带到另一个django视图:
def another_view(request):
Event.objects.filter(member = user1)
我的jquery代码是这样的:
$( "#acounts" ).autocomplete({
source: "{% url 'search_view' %}",
focus: function( event, ui ) {
$( "#acounts" ).val( ui.item.label );
return false;
select: function( event, ui ) {
$( "#accounts").val(ui.item.label );
$( "#accounts-id").html( ui.item.label );
return false;
和html:
<input id="accounts">
<p id="accounts-id"></p>
答案 0 :(得分:0)
url.py
url(r'^api/user/(?P<id_user>w+)/$', views.another_view,name="another_view"),
views.py
def another_view(request,id_user):
Event.objects.filter(member = id_user)
# do stuff
# return stuff
jquery的:
var yourTerm = "something";
$.get( "api/autocomplete/yourobject/" + yourTerm, function( users) {
// users = for example: ["user1", "user2"]
// could be get or post or whatever depending what you are trying to do
$.get( "api/user/" + users[0], function( data ) {
// data is what your view responds
});
});