On Jan 27, 10:38 am, Ashok Prabhu <ashokprab...@gmail.com> wrote:
> Hi,
>
> I have the following view which throws the error "The view
> TCDB.viewdb.views.dbout didn't return an HttpResponse object." I have
> tried various indentation for the return statement. But it doesn't
> seem to help. Can somebody help?
>
> def dbout(request):
>  try:
>   filter_string= request.GET.get('filter_string', '')
>   table_name=request.GET.get('table_name','')
>   field_name=request.GET.get('field_name','')
>   if field_name=='hostid':
>    results=eval(table_name).objects.filter(hostid__icontains=str
> (filter_string)).values()
>   elif field_name=='hostname':
>    results=eval(table_name).objects.filter(hostname__icontains=str
> (filter_string)).values()
>   elif field_name=='plat_manu_name':
>    results=eval(table_name).objects.filter
> (plat_manu_name__icontains=str(filter_string)).values()
>   elif field_name=='mac':
>    results=eval(table_name).objects.filter(mac__icontains=str
> (filter_string)).values()
>   return HttpResponse('No Exception')
>  except:
>   return HttpResponse('Exception Reached')
>
> Thanks,
> ~Ashok.

Firstly, why are you using a single character indent? If you used the
standard four-space indent, it would be much much easier to see your
blocks. Since, as you have discovered, indentation is critical in
Python, it makes sense to make it visible. In fact, a good reading of
PEP8 would make your code much more readable generally.

Please re-post your code with a decent indent, so we can see what's
going on.

Two other pointers which may help you generally:

Firstly, don't use a bare 'except'. You might be hiding all sorts of
errors. If you're expecting an exception, catch that specific one and
deal with it, and leave others to bubble up. They will be displayed
nicely on the Django error page.

Secondly, don't 'eval' strings coming directly from the user (ie in
request.GET). That's an open door for a hacker to run arbitrary code
on your system. Instead, use a dictionary of strings to look up
models:

    model_dict = {'model1': Model1, 'model2': Model2}
    results = model_dict[table_name].objects.filter(...)

--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to