Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:04:15+00:00 2026-06-02T02:04:15+00:00

I am trying to change to class-based views in Django after an upgrade and

  • 0

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()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:04:16+00:00Added an answer on June 2, 2026 at 2:04 am

    You are seeing name 'SearchView' is not defined because you have not imported SearchView into your urls.py. If you think this is ugly then you can do search = SearchView.as_view() in your views.py and use the old style to reference the view as search instead. The request can be accessed on self.request for adding the messages. The updated source example is given below.

    # views.py
    class SearchView(TemplateView):
        template_name = 'search.html'
    
        def get_context_data(self, **kwargs):
            messages.success(self.request, 'test')
            return {'search_string': 'Test'}
    
    search = SearchView.as_view()
    
    # urls.py
    urlpatterns += patterns('project.app.views',
        url('^$', 'index'), # Old style
        url(r'^test/$', 'search'), # New style
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to update the Django User model with the class based UpdateView
I have the following code based on this great template for making custom WordPress
I am trying to change the class of a element with Javascript using the
I am trying to change the property type in interface implementation class using explicit
I am trying to access/change the properties of a class' Parent through Reflection. If
currently i am trying to laod *.obj-files into my programm. My code is based
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have got two models, seen below, and am trying to insert one of
I'm trying to get a couple spinners to change dynamically based on their previous
I'm trying to pass in a custom argument to the Django Admin change list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.