tags:

views:

137

answers:

2

Hi! I'm using dango 0.97 pre (yep, my site still was not upgraded to be compatible with trunk). I use apache + fastcgi for it. And I have a strange problem: sometimes (I can't guess the order) some of the views throw such error:

Tried audio_index in module mysite.audio.views. Error was: 'module' object has no attribute 'validators'

There is no any calls of validators in this view:

def audio_index(request, language):
audio_list = AudioFile.objects.all().filter(lang = language).order_by('-title')
audio_list = list(audio_list)[:10]
sections_list = AudioCategory.objects.all().filter(lang = language)  
template_name = 'audio/index_' + language + '.html'
t = loader.get_template(template_name)
c = Context({
    'sections_list': sections_list,
    'lang': language,
    'audio_list': audio_list,
    'len1': len(audio_list),
    'menu_item': 'audio', 
})
return HttpResponse(t.render(c))

The most interesting fact is that if I will reload this page after some time, it will load absolutlely correct (I don't change any code or data in database before it). Do you have any ideas, what is it? Maybe it is non-django problem (apache? db?)?

The full stacktrace is as follows:

Traceback:
File "/home/shal_prod/django/django_src/django/core/handlers/base.py" in get_response
  73.             callback, callback_args, callback_kwargs = resolver.resolve(request.path)
File "/home/shal_prod/django/django_src/django/core/urlresolvers.py" in resolve
  233.                     sub_match = pattern.resolve(new_path)
File "/home/shal_prod/django/django_src/django/core/urlresolvers.py" in resolve
  172.             return self.callback, args, kwargs
File "/home/shal_prod/django/django_src/django/core/urlresolvers.py" in _get_callback
  184.             raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))

Exception Type: ViewDoesNotExist at /ru/about/
Exception Value: Tried index in module mysite.about.views. Error was: 'module' object has no attribute 'validators'
+1  A: 

I can't answer your problem (except to say please post your models code, as I suspect the problem is there), but can I point out that these two lines:

audio_list = AudioFile.objects.all().filter(lang = language).order_by('-title')
audio_list = list(audio_list)[:10]

are horrifyingly inefficient. What you're doing here is getting all AudioFile objects, instantiating them, and then throwing away all but 10. What you should be doing is simply this:

audio_list = AudioFile.objects.all().filter(lang=language).order_by('-title')[:10]

so that Django slices the queryset before evaluating it, and only asks the database for 10 objects to begin with. You can always convert it to a list after slicing, if you really really need to.

Daniel Roseman
+1 this is a great point, limiting on the origional query will make your application far faster as the AudioFile DB increases in size.
Tom Leys
A: 

This is very difficult to debug without a stack trace from your application.

What the error message means is that one of your modules expects another module to contain a variable called "validators"

My guess would be that you are assigning to a global variable of that name in some method and then expecting it to be present in another method. Try to find the keyword validators in your code and see where it is used.

edit: Based on the original poster's stacktrace, it looks like the view that handles a URL is not being imported. It looks like views use URL strings using names, not module functions.

Try changing from this pattern of declaring URLS

urlpatterns = patterns('',
    (r'^admin/(.*)', 'admin.site.root'),

to this pattern

import admin.site
urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root), #note string is gone here
Tom Leys
Tom, see stacktrace here: http://pastebin.com/m1db30571
dbf