开发者

Displaying a messge when search not found

开发者 https://www.devze.com 2023-02-17 08:10 出处:网络
I have done a filtering method using Django. The filtering is based on a choice selected from a drop down menu and a text input. the filtering is working fine. Now i want to display a message if the s

I have done a filtering method using Django. The filtering is based on a choice selected from a drop down menu and a text input. the filtering is working fine. Now i want to display a message if the search is not found. Can somebody help me to solve this. Will post my code here.

def filter(request):
    val3='' 
    if request.GET.has_key('choices'):
        val2=request.GET.get('choices')
    if request.GET.has_key('textField'):
        val3=request.GET.get('textField')
开发者_如何学Go    if request.POST:
        val2=request.POST.get('choices')    
        val3=request.POST.get('textField')
    if val2=='Designation':                
        newData = EmployeeDetails.objects.filter(designation=val3) 
        flag=True 
    elif val2=='Name':
        newData = EmployeeDetails.objects.filter(userName__icontains=val3)
        flag=True 
    elif val2=='EmployeeID':
        newData = EmployeeDetails.objects.filter(employeeID=val3)  
        flag=True       
    elif val2=='Project':
        newData = EmployeeDetails.objects.filter(project=val3)   
        flag=True   
    elif val2=='DateOfJoin':
        newData = EmployeeDetails.objects.filter(dateOfJoin=val3) 
        flag=True       
    else:
        return HttpResponseRedirect('/employeeList/')

html

<h4 align="left">
{%for data in newData%}
<a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{data.id}}?choices={{val2}}&textField={{val3}}&flag=1 ">
{{ data.userName}}<br>
{%endfor%} 
</h4>


check newdata in filter view or in html ... if its empty show your msg

example :

{% if newData %}
    newData
{% else %}
    error
{% endif %}
  • anything is anything you want check is empty?


How about display a message if the flag was not set to true or if newData is null when you can't find anything?

{% if flag is None or flag == False or newData is None %}
    <span>Error Message</span>
{% endif %}

You might want to check to see if it's necessary to check for flag is null too... can't tell from your posted code if that is necessary.

HTH


If you're a little bit 'clever' with how you name your get/post variables, you can minimize your view code significantly.

def filter(request):
    fieldname = request.GET.get('choices', None) or request.POST.get('choices')
    value = request.GET.get('textField', None) or request.POST.get('textField')

    if fieldname is None or value is None:
        return HttpResponseRedirect('/employeeList/')

    new_data = EmployeeDetails.objects.filter(**{fieldname:value})
    # use new_data for your purposes here, without the flag

However, you need to make sure that the GET or POST variable names in choices exactly match (including case) the field names of your model.

Then in your template:

<h4 align="left">
{%for data in new_data %}
    <a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{data.id}}?choices={{val2}}&textField=   {{val3}}&flag=1 ">
    {{ data.userName}}<br>
{%endfor%}
{% if new_data is None %}
    <div class='error'> No Data </div>
{% endif %}
</h4>
0

精彩评论

暂无评论...
验证码 换一张
取 消