Django TemplateSyntaxError

I am following a Django tutorial, and all of a sudden, when I try to access http://127.0.0.1:8000/admin/ , it gives me a TemplateSyntaxError.

TemplateSyntaxError at / admin /

Caught ViewDoesNotExist on rendering: Tried the results in the polls.views module. Error: object 'module' has no attributes 'results'

He highlights this line: {% url 'django-admindocs-docroot' as docsroot%}

The admin page worked like a charm until I got to part 3 of the tutorial and messed up the urls, although I did it exactly as they said, so I doubt this problem.

urls.py:

from django.conf.urls.defaults import * from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', (r'^polls/$', 'polls.views.index'), (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'), (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'), (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'), (r'^admin/', include(admin.site.urls)), ) 

admin.py:

 from polls.models import Poll from polls.models import Choice from django.contrib import admin class ChoiceInline(admin.TabularInline): model = Choice extra = 0 class PollAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['question']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline] list_display = ('question', 'pub_date') list_filter = ['pub_date'] search_fields = ['question'] date_hierarchy = 'pub_date' admin.site.register(Poll, PollAdmin) 

views.py:

 from django.http import HttpResponse from polls.models import Poll from django.template import Context, loader def index(request): latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5] t = loader.get_template('polls/index.html') c = Context({ 'latest_poll_list': latest_poll_list, }) return HttpResponse(t.render(c)) def detail(request, poll_id): return HttpResponse("You're looking at poll %s. " % poll_id) def vote(request, poll_id): return HttpResponse("You're voting on poll %s." % poll_id) 
+8
python django
source share
1 answer
 Caught ViewDoesNotExist while rendering: Tried results in module polls.views. Error was: 'module' object has no attribute 'results' 

That is almost all you need. Ignore TemplateSyntaxError , it is not related to the template at all. Django tells you that you do not have this:

 def results(request): # do something 

In your view.py. You will get ViewDoesNotExist errors outside of the administrator when you start writing URLs and referring to functions that do not actually exist in them, so make sure that as you go along, you either guarantee that you have such functions - stubs that return only base 200, or you will comment on these URLs until you need them.

Technically speaking, this is an extension of the python error. If you run:

 $ python manage.py shell >>> from poll import views x = views.results 

You will get an AttributeError .

Since you asked why, if you look in Django/core/urlresolvers.py , you will see the line:

 _callable_cache = {} # Maps view and url pattern names to their view functions. 

Thus, basically the cache of representations (URLs, etc.) for functions is performed in the form of a hash map (dictionary). This is built using this function:

 def _get_callback(self): if self._callback is not None: return self._callback try: self._callback = get_callable(self._callback_str) except ImportError, e: mod_name, _ = get_mod_func(self._callback_str) raise ViewDoesNotExist("Could not import %s. Error was: %s" % ( mod_name, str(e))) except AttributeError, e: mod_name, func_name = get_mod_func(self._callback_str) raise ViewDoesNotExist("Tried %s in module %s. Error was: %s" % ( func_name, mod_name, str(e))) return self._callback callback = property(_get_callback) 

Who evaluates each callback to check for its existence (newlines are mine). A.

+15
source share

All Articles