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 8936779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:17:26+00:00 2026-06-15T10:17:26+00:00

I am learning web application development, and am trying out the separating of modules

  • 0

I am learning web application development, and am trying out the separating of modules just to keep my program more obviously object oriented and easy to navigate / understand.

My first import in Main.py is working fine:

import jinja2
import main_page     # <<<- This import of my module works
import os
import webapp2
from string import letters

#   loads templates to make our life easier
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                               autoescape = True)

########    Main App Function   ########
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

My second import in main_page.py isn’t working:

import main_handler  # <<< -- This import is not working

########    Implementation      ########    

#   main page handler
class MainPage(BaseHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        visits = 0
        visit_cookie_str = self.request.cookies.get('visits')
        if visit_cookie_str:
            cookie_val = check_secure_val(visit_cookie_str)
            if cookie_val:
                visits = int(cookie_val)

        visits += 1     
        new_cookie_val = make_secure_val(str(visits))
        self.response.headers.add_header('Set-Cookie', 'visits=%s' % new_cookie_val)
        self.write("You've been here %s times and this is the cookie: \n %s" % (visits, new_cookie_val))

I get this error in the terminal:

File “/Users/James/Developer/Google-App-Engine/Cookies/main_page.py”,
line 6, in
class MainPage(BaseHandler): NameError: name ‘BaseHandler’ is not defined

I’ve tried changing file names and class names inc ase they were getting confused with other modules. The files are all in the same folder.

This is the **main_handler.py**:

import webapp2
import string
import jinja2
import hashlib
import hmac

SECRET = "test secret"

#   global hash functions
def hash_str(s):
    return hmac.new(SECRET, s).hexdigest()

def make_secure_val(s):
    return "%s|%s" % (s, hash_str(s))

def check_secure_val(h):
    val = h.split('|')[0]
    if h == make_secure_val(val):
        return val 

#   this is a template with convenience methods
class BaseHandler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)   

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
  • 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-15T10:17:27+00:00Added an answer on June 15, 2026 at 10:17 am

    When you import “main_handler”, you can use it like this:

    main_handler.make_secure_val
    

    Not like this:

    make_secure_val
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just into learning the basics of web application development. As i was
I am just beginning to start learning web application development, using python. I am
I'm trying to setup a new web-application for learning purposes using the newest technologies:
I'm interested in learning how to design/plan for web application development, in a multiple
I'm working on a small Servlets & JSP application while learning web development. I
I am learning web application development. To save anything on a server, (In the
I just start learning to write web services yesterday, kinda fascinating. I created an
I am learning to how send email with Rails 3 with Agile Web Development
I am a C/C++ programmer with moderate experience in desktop application. (no web development).
I'm a long time hobbyist programmer interested in getting into web application development. I

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.