在这一步中,我试图避免制作一个将数据从前端发送到django的表单。 理想情况下,我想使用一个onclick按钮将一个简单的字符串传递给queryset,这样我就可以在Post模型上创建标签过滤器,并使用来自字符串输入的标签显示标签过滤器。 (只需将字符串发送到queryset,我就可以处理split()和strip()以使其有效)
简化的html类似于
AndroidNetworking.upload(url)
.addMultipartParameter("user_id",dataManager!!.userId)
.addMultipartParameter("property_id",dataManager!!.propertyId)
.addMultipartParameter("firstname",dataManager!!.firstname)
.addMultipartParameter("lastname",dataManager!!.lastname)
.addMultipartParameter("questionsModel",editFormRequest.questionsModels) //here i want put my List whith objects,but function not receive this parameter
.setPriority(Priority.HIGH)
.build()
.getAsObject(EditFormResponse::class.java,object :ParsedRequestListener<EditFormResponse>{
override fun onResponse(response: EditFormResponse?) {
Log.d("editForm","onResponse message " + response!!.message)
progressBar.invisible()
}
override fun onError(anError: ANError?) {
Log.d("editForm","onError message " + anError!!.message)
toast(getString(R.string.server_error))
progressBar.invisible()
}
})
和ListView
<input type="text" name="tag_filter">
<button type="button" onclick="Tag_Filter()">Filter</button>
<script>
function Tag_Filter() {
var tags;
tags = document.getElementById("tag_filter");
}
</script>
urls.py
class PostListView(ListView):
model = Post #.objects.filter(tags__name__in=["black&white", "red"]).distinct()
template_name = 'post/home.html' # <app>/<model>_<viewtype>.html
#queryset = Post.objects.all()
context_object_name = 'posts'
#ordering = ['-date_posted']
def get_queryset(self):
tag_list = #the return from the Tag_Filter() js
return Post.objects.filter(tags__name__in=tag_list).annotate(num_tags=Count('tags')).filter(num_tags__gte=len(tag_list)).order_by('-date_posted').distinct()
答案 0 :(得分:0)
我建议使用ajax,然后第一步是实现ajax ... ajax来自Jquery,那么您只需要在模板上为一个文件html包含jquery,或者如果您更喜欢在文件末尾的base.html中添加jquery在您的身体结束标记之前:
<!-- jQuery library -->
<script src="{% static 'Jquery/jquery.min.js' %}"></script>
....
...
all my files js
....
</body>
</html>
要下载此jquery.min.js,请转到此链接并将文件保存到文件夹:static / Jquery /:
Link for download Jquery min.js
别忘了另存为 jquery.min.js
接下来是设置您的settings.py以接受静态文件:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/static/',
]
接下来,根据需要,将用于加载静态文件的标签放在文件html或base.html的顶部:
{% load staticfiles %}
<!DOCTYPE html>
<html>
...
...
...
<!-- jQuery library -->
<script src="{% static 'Jquery/jquery.min.js' %}"></script>
....
...
all my files js
....
</body>
</html>
好吧,现在我们包含 $。ajax ,首先您需要在按钮上包含一个函数,该函数可以从输入中获取值:
<input type="text" name="tag_filter" id ="tag_filter">
<button type="button" onclick="tag_filter()">Filter</button>
现在您的函数tag_filter:
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
//RETURN YOUR TOKEN FROM DJANGO FOR SEND FROM AJAX
return cookieValue;
}//end function getCookie
function tag_filter(){
var tag_filter = $("#tag_filter").val();
if(tag_filter == '' || tag_filter == null){
alert('write yout filter for search !!');
return false;
}
//now we include the ajax call
//token
var csrftoken = getCookie('csrftoken');
$.ajax({
type: "GET",
url: "search_filter",
data:{
csrfmiddlewaretoken : csrftoken,
tag_filter:tag_filter,
},
dataType: "json",
success: function(data) {
code = data.result;
if(code=='ok_select'){
// here write you code for the success answer from ajax call
console.log(data);
}
},
error: function( jqXHR, textStatus, errorThrown ) {
alert('Error ' + jqXHR.responseText);
}//end error
});
}
现在您需要使用参数示例创建搜索用的def:
from django.http import HttpResponse, JsonResponse
...
..
def SearchFilter(request):
filter = request.GET['tag_filter']
....
....
data ={
'result':'ok_select'
'data': #your query
}
return JsonResponse(data, safe=False)
现在您需要在您的urls.py示例中声明此定义:
from myapp.views import SearchFilter
urlpatterns = [
url(r'^search_filter', SearchFilter, name='search_filter'),
...
...
]
这就是全部...对于ajax,通过控制台返回成功答案,请从您的Web浏览器收藏夹中在控制台上检查此答案。
祝你好运.. !!