I am trying to change to class-based views in Django after an upgrade and I have two questions regarding this. This is my code, simplified:
# urls.py
urlpatterns += patterns('project.app.views',
('^$', 'index'), # Old style
url(r'^test/$', SearchView.as_view()), # New style
)
# views.py
class SearchView(TemplateView):
template_name = 'search.html'
def get_context_data(self, **kwargs):
messages.success(request, 'test')
return {'search_string': 'Test'}
When I run this I first get the error name 'SearchView' is not defined
. Does anyone know why?
Trying to skip that I add from project.app.views import SearchView
which is ugly and not the way I want it to work, but hey, I try to see if I can get the rest working. Then I get global name 'request' is not defined
because of the messages
. This makes sense but how do I get the request object here?
So I'd like to know: how do I get the views to work as intended and how to use messages in get_context_data()
?