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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:03:57+00:00 2026-05-27T17:03:57+00:00

My code: #!/bin/python import os,sys from datetime import datetime from flask import Flask from

  • 0

My code:

#!/bin/python
import os,sys
from datetime import datetime
from flask import Flask
from database import db_session,init_db
from models import Node

version = '0.1'
app = Flask(__name__)

@app.route("/")
def index():
    return "hello"

@app.route("/add")
def add():
    node = Node('test','test','this is a test',1)
    db_session.add(node)
    db_session.commit()
    return 'is ok'

@app.teardown_request
def shutdown_session(exception=None):
    print "Teardown 1 {0!r}".format(exception)
    db_session.remove()


if __name__ == "__main__":
    app.run(debug=True)

my models.py,this is a simple models ,just a Node

from sqlalchemy import Column,Integer,String,Text
from database import Base

class Node(Base):
    __tablename__ = 'nodes'

    id = Column(Integer, primary_key=True)
    title = Column(String(300))
    tagnames = Column(String(125))
    body = Column(Text())
    nodetype=Column('node_type',Integer(11))

    def __init__(self,title=None,tagnames=None,body=None,nodetype=0):
        self.title = title
        self.tagnames = tagnames
        self.body = body
        self.nodetype = nodetype

    def __repr__(self):
        return '<Node %r>' % (self.title)

my database.py,I’m not using flask-sqlalchemy

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('mysql://root:123456@127.0.0.1:3306/test', echo=True,convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    import flaskq.models
    Base.metadata.create_all(bind=engine,checkfirst=True)

when I first request “http://127.0.0.1:5000/add&#8221;
this code throws:
AttributeError: ‘NoneType’ object has no attribute ‘request’

request again,every things is ok.

  • 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-05-27T17:03:58+00:00Added an answer on May 27, 2026 at 5:03 pm

    Obviously you lack an object which should have a request attribute. It’s possible, that somewhere in your add-method you need a reference to the request-object/context. (Is this really the whole Error-Report? Partial Error-Reporst are seldom useful!) I guess your using the django libraries here, and maybe Flask with SQLite3-Example might be useful to you. Through the teardown_request a request-context is automatically established and that maybe the reason it just fails at the first call.

    According to Context-Locals there is sometimes a need for an explicit request-context (whatever that means in detail i can’t explain) maybe the following will help:

    from flask import request
    
    ... 
    def add():
       with app.request_context(environ):
          node = Node('test','test','this is a test',1)
          db_session.add(node)
          db_session.commit()
          return 'is ok'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have that code: #!/usr/bin/python -u localport = 9876 import sys, re, os from
I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore
Simple code demonstrating the problem: #!/usr/bin/env python import sys from PyQt4.QtCore import QObject, SIGNAL
So I found this code: #!/usr/bin/python import sys #for cmd line argv import time
here is my code #!/usr/bin/python # -*- coding:utf-8 -*- import sys import pysvn def
#! /usr/bin/env python import os import stat import sys class chkup: def set(file): filepermission
The code #!/usr/bin/env python import MySQLdb print Content-Type: text/html print print <html><head><title>Books</title></head> print <body>
My code: #!/usr/bin/env python def Runaaall(aaa): Objects9(1.0, 2.0) def Objects9(aaa1, aaa2): If aaa2 !=
I've got python code of the form: (o,i) = os.popen2 (/usr/bin/ssh host executable) ios
Imagine the following folder structure: project src code.c makefile bin How can I compile

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.