通过基于类的视图添加搜索栏

时间:2019-05-27 14:20:14

标签: python django

我是django的新手,我想在我的网络中创建一个搜索栏。在我的网站上,我显示了form.ModelMultipleChoiceField的列表。在我将搜索栏添加到我的网站后,列表不变,不会更改为我要搜索的内容。

这是我在html文件中的表单:

<form method="GET" action="{% url 'diagnosis_penyakit:search' %}">
   <p style="margin-left: 700px;">Gejala : </p>
   <input type="text" name="q" value="{{ request.GET.q }}" id="data" class="form-control" placeholder="Cari gejala disini" style="width: 500px; margin-left: 700px">
   <button class="btn btn-success" type="submit" style="margin: 3rem; margin-left: 900px">
        Search
   </button>
</form>

这是我的views.py:

def search(request):
template = 'penyakit/home.html'

query = request.GET.get('q')

results =  Gejala.objects.filter(Q(gejala__contains=query))

print(results, "search test")

if request.user.is_authenticated:
    form_review = ReviewUserForm(request.POST or None)
    form = UserResponseForm(request.POST or None)
    list_rev_user = []
    rev_user = ReviewFromUser.objects.all().filter(user_id=request.user.id)
    for rev in rev_user:
        list_rev_user.append(rev.number_diagnosis)
    num_diagnosis = UserAnswer.objects.all().filter(user_id = request.user.id).order_by("-number_diagnosis")
    num = list_rev_user.count(num_diagnosis[0].number_diagnosis)
    if len(num_diagnosis)>0:
        user_answer = UserAnswer.objects.all().filter(user_id = request.user.id, number_diagnosis = num_diagnosis[0].number_diagnosis)
        diagnosis = DiagnosisPenyakit.objects.all().filter(user_id = request.user.id, number_diagnosis = num_diagnosis[0].number_diagnosis).order_by("-total_point")
        context = {
            "form": form,
            "form_review" : form_review,
            "user_answer" : user_answer,
            "diagnosis" : diagnosis,
            "hasil_diagnosis": diagnosis[0],
            "num" : num,
            "query" : query,
        }
        return render(request, "penyakit/home.html", context)
    else:
        context = {
            "form": form,
        }
        return render(request, "penyakit/home.html", context)
else:
    raise Http404

return render_to_response(request, template, context)

这是我的url.py:

url(r'^results/$', views.search, name='search'),

这是我的forms.py:

class UserResponseForm(forms.Form):
     gejala_id1 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=1).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
     gejala_id2 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=2).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
     gejala_id3 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=3).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
     gejala_id4 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=4).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)
     gejala_id5 = forms.ModelMultipleChoiceField(queryset=Gejala.objects.all().values_list('gejala', flat=True).distinct().filter(id_organ=5).order_by('gejala'), widget=forms.CheckboxSelectMultiple, required=False)

我想添加一个搜索栏,并尝试了上面的搜索栏,但它不起作用。希望有人可以帮助我在我的网络中添加搜索栏。正如我已经说过的,我对django非常陌生。

0 个答案:

没有答案