I'm trying to build my first GAE app with jinja2. After overcoming a dozen small errors, now I'm stuck with this:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\CG\Documents\udacity\HiMon\main.py", line 31, in get
template = jinja_environment.get_template('index.html')
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 719, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 693, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 180, in get_source
raise TemplateNotFound(template)
TemplateNotFound: index.html
Here my yaml file:
application: himother
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.1"
- name: jinja2
version: "2.6"
Here my code:
import os
import webapp2
import jinja2
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
class MainPage(webapp2.RequestHandler):
def get(self):
template_values = {
'name': 'Serendipo',
'verb': 'extremely happy'
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
Here my .html template:
<!DOCTYPE html>
<html>
<head>
<title>Look Ma, I'm using Jinja!</title>
</head>
<body>
Hi there - I'm {{ name }}, and I {{ verb }} programming!
</body>
</html>
Despite the error message, I have a folder called "templates" and, within it, created the index.html file:
I also have installed jinja2.
Does anyone have any idea of the cause of this error now?
Try to use
loader=jinja2.FileSystemLoader('templates')
instead of
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))
It works for me.
This solved it for me:
mkdir templates
echo "hello world" > templates/index.html
Well, my error was simple and silly.
I have create the file "index.html" the wrong way (here the right way). So, my "index.html" file was indeed a ".text" file (because I just rename it to "index.html" instead of "save as" index.html").
Thanks for the help, guys!
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)
Two thoughts based on getting my first GAE effort with Jinja2 to work. First, in your yaml file, you have "-url: ." though I used "-url: /." based on tutorials I saw. However, this may be irrelevant to your issue. Second, I used the guidance on this page for how I established my Jinja2 renderer and had no issue with the template being found in the templates subdirectory of the application directory:
http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html#module-webapp2_extras.jinja2
I received the same error and tried all answers. Later, I got to know that you first need to configure the environment with the right folder to search the html file and only then jinja2 will be able to locate the file.
Following lines will remove the error:
env = Environment(loader=FileSystemLoader(searchpath='C:\Folder\of\html\file')
template = env.get_template('Name_of_file.html')
Related
I'm new to Flask and I'm trying to learn how to use it. I have a base knowledge of HTML and CSS (enough to make simple pages, not something really fancy).
So, I decided to give Flask a shot, it looks fairly simple to setup at least the basics.
For the moment I'm trying to run a local server (I'm using Windows 7,Python 3.4). Based on some instructions, I set up the project like this:
-Flask (main folder)
routes.py
/templates
template.html
home.html
welcome.html
/static
/css
/img
/js
/font (for the moment the static folder is empty, except the subfolders)
This is the structure of template.html:
<!--DOCTYPE html -->
<html>
<head>
<title>
My first Flask App
</title>
</head>
<body>
<div>
{% block content %}
{% endblock %}
</div>
</body>
</html>
home.html:
{% extends "template.html" %}
{% block content %}
<div>
<h2>Welcome to Flask!</h2>
<br />
<p>Click hereto go to the welcome page</p>
</div>
{% endblock %}
And finally, welcome.html:
{% extends "template.html" %}
{% block content %}
<h2>Sample</h2>
<p>What a nice page</p>
<p>Really nice</p>
{% endblock %}
This is the code in routes.py:
from flask import Flask,render_template
app=Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
#app.route('/welcome')
def welcome():
return render_template('welcome.html')
if __name__=='__main__':
app.run()
When I try to run the script, the terminal says:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
But, when I try to go to localhost:5000 on the browser, I got:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
More specifically, on the terminal says this:
127.0.0.1 - - [30/Jan/2016 14:45:51] "GET / HTTP/1.1" 500 -
I know a little about HTTP status code, so I know that code 500 is raised when there is some sort of error. Indeed, when i looked up for information about this problem, every page I found (included here on stackoverflow) suggests that there is some error, either in "routes.py" or in the html pages provided.
Just to be sure, I tried to run this script:
from flask import Flask
app=Flask(__name__)
#app.route('/')
def home():
return '''
<html>
<head>
<title>
My first Flask App
</title>
</head>
<body>
<div>
hello there!
</div>
</body>
</html>
'''
if __name__=='__main__':
app.run()
And this works fine, the server is booted and if I go to localhost:5000 I can see the page correctly. Indeed, on the terminal I see the status code 200, that means "all is ok", if I recall correctly.
I even tried to add more routes, by defining more functions that return the html pages as strings, and all works fine.
I even tried on those pages to use CSS, in the three forms (in line declaration, internal style sheet and external style sheet), it all works as expected.
So, for the last try, i run the original routes.py once again, this time with app.debug=True
This is the traceback that i got:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "C:\Users\Admin\pyproj\flask\routes.py", line 25, in <module>
app.run()
File "C:\Python34\Lib\site-packages\Flask-0.10.1-py3.4.egg\flask\app.py", line 777, in run
run_simple(host, port, self, **options)
File "C:\Python34\Lib\site-packages\werkzeug-0.11.3-py3.4.egg\werkzeug\serving.py", line 690, in run_simple
reloader_type)
File "C:\Python34\Lib\site-packages\werkzeug-0.11.3-py3.4.egg\werkzeug\_reloader.py", line 252, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
builtins.SystemExit: 0
Seriously, there must be something really obvious that I'm missing here: I checked all the codes countless times. To me, all seems fine: all the tags in the HTML documents are properly closed, the {%---%} syntax for the templates seems good written, the code on routes.py seems correct also. Furthermore, it generally seems that Flask is working, because if I try to return the html pages as strings written direcly in routes.py I can see and interact with the pages. What's going on?
EDIT
Based on davidism suggestion, I tried to run the script with app.run(debug=True,use_reloader=False)
And this is the traceback
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "x-wingide-python-shell://70495568/2", line 16, in home
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\templating.py", line 127, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Python34\lib\site-packages\jinja2-2.8-py3.4.egg\jinja2\environment.py", line 851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Python34\lib\site-packages\jinja2-2.8-py3.4.egg\jinja2\environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Python34\lib\site-packages\jinja2-2.8-py3.4.egg\jinja2\environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
File "C:\Python34\lib\site-packages\flask-0.10.1-py3.4.egg\flask\templating.py", line 64, in get_source
raise TemplateNotFound(template)
I can see the traceback on the browser aswell.
EDIT 2:
Thanks to davidism, I tried to run the script from the command line and it works perfectly. Normally I use WingIDE for writing python code, debugging and so on; but in this particular case it seems to conflict with Jinja2. Indeed, I tested several times now, and every time I use WingIDE I get the 500 status code, when I run the script from the command line (even if I simply double-click on the script) it works fine.
I am competing in the Google App Challenge but I am unable to get the app working at the appspot domain website.I am using Python to deploy the app.Need Help
The app.yaml file details are given below
application: abhicorp111
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /WebPlayer\.unity3d
static_files: WebPlayer/WebPlayer.unity3d
upload: WebPlayer/WebPlayer\.unity3d
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
The main.py content
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.ext.webapp import template
import webapp2
import os
class MainHandler(webapp2.RequestHandler):
def get(self):
template_values = {
'greetings': 'greetings',
}
path = os.path.join('WebPlayer', 'WebPlayer.html')
self.response.out.write(template.render(path, template_values))
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
After updating from command line getting the 500 server error
The Folder structure for the app
The error that i am getting
The error log i got while uploading it through GAE log Console
> 2013-11-20 09:27:34 Running command: "['C:\\Python27\\python.exe',
> 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py',
> '--skip_sdk_update_check=yes', '--port=15080', '--admin_port=8007',
> u'E:\\GAEGame\\WebPlayer']" INFO 2013-11-20 09:27:35,315
> devappserver2.py:660] Skipping SDK update check. WARNING 2013-11-20
> 09:27:35,331 api_server.py:327] Could not initialize images API; you
> are likely missing the Python "PIL" module. WARNING 2013-11-20
> 09:27:35,588 simple_search_stub.py:1009] Could not read search indexes
> from
> c:\users\santu\appdata\local\temp\appengine.abhicorp111\search_indexes
> INFO 2013-11-20 09:27:35,648 api_server.py:138] Starting API
> server at: http://localhost:4755 INFO 2013-11-20 09:27:35,674
> dispatcher.py:168] Starting module "default" running at:
> http://localhost:15080 INFO 2013-11-20 09:27:35,700
> admin_server.py:117] Starting admin server at: http://localhost:8007
> ERROR 2013-11-20 03:57:44,651 wsgi.py:262]
>
> Traceback (most recent call last):
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 239, in Handle
>
> handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 298, in _LoadHandler
>
> handler, path, err = LoadObject(self._handler)
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 84, in LoadObject
>
> obj = __import__(path[0])
>
> File "E:\GAEGame\WebPlayer\main.py", line 22
>
> def get(self):
>
> ^
>
> IndentationError: expected an indented block
>
> INFO 2013-11-20 09:27:44,663 module.py:599] default: "GET /
> HTTP/1.1" 500 - ERROR 2013-11-20 03:57:44,697 wsgi.py:262]
>
> Traceback (most recent call last):
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 239, in Handle
>
> handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 298, in _LoadHandler
>
> handler, path, err = LoadObject(self._handler)
>
> File "C:\Program Files
> (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line
> 84, in LoadObject
>
> obj = __import__(path[0])
>
> File "E:\GAEGame\WebPlayer\main.py", line 22
>
> def get(self):
>
> ^
>
> IndentationError: expected an indented block
>
> INFO 2013-11-20 09:27:44,710 module.py:599] default: "GET
> /favicon.ico HTTP/1.1" 500 -
After updating and going through the errors and then starting the server i am getting this message
This is done from another folder and then i called the appserver start command of python
C:\Windows\system32>dev_appserver.py E:\ed23\WebPlayer1
INFO 2013-11-20 13:25:04,336 sdk_update_checker.py:245] Checking for updates
to the SDK.
WARNING 2013-11-20 13:25:06,148 api_server.py:327] Could not initialize images
API; you are likely missing the Python "PIL" module.
WARNING 2013-11-20 13:25:06,641 simple_search_stub.py:1009] Could not read sear
ch indexes from c:\users\santu\appdata\local\temp\appengine.enginerite11\search_
indexes
INFO 2013-11-20 13:25:06,688 api_server.py:138] Starting API server at: http
://localhost:20091
INFO 2013-11-20 13:25:06,713 dispatcher.py:168] Starting module "default" ru
nning at: http://localhost:8080
INFO 2013-11-20 13:25:06,742 admin_server.py:117] Starting admin server at:
http://localhost:8000
ERROR 2013-11-20 07:58:37,220 webapp2.py:1552] WebPlayer1.html
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 570, in dispatch
return method(*args, **kwargs)
File "E:\ed23\WebPlayer1\main.py", line 28, in get
self.response.out.write(template.render(path, template_values))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\weba
pp\template.py", line 89, in render
t = _load_internal_django(template_path, debug)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\weba
pp\template.py", line 163, in _load_internal_django
template = django.template.loader.get_template(file_name)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\_interna
l\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\_interna
l\django\template\loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: WebPlayer1.html
INFO 2013-11-20 13:28:37,285 module.py:599] default: "GET / HTTP/1.1" 500 24
09
INFO 2013-11-20 13:28:37,497 module.py:599] default: "GET /favicon.ico HTTP/
1.1" 200 1150
ERROR 2013-11-20 08:03:19,309 webapp2.py:1552] WebPlayer1.html
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2
.py", line 570, in dispatch
return method(*args, **kwargs)
File "E:\ed23\WebPlayer1\main.py", line 28, in get
self.response.out.write(template.render(path, template_values))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\weba
pp\template.py", line 89, in render
t = _load_internal_django(template_path, debug)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\weba
pp\template.py", line 163, in _load_internal_django
template = django.template.loader.get_template(file_name)
INFO 2013-11-20 13:33:19,326 module.py:599] default: "GET / HTTP/1.1" 500 24
09
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\_interna
l\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\_interna
l\django\template\loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: WebPlayer1.html
INFO 2013-11-20 13:33:19,427 module.py:599] default: "GET /favicon.ico HTTP/
1.1" 304 -
main.py has syntax errors (incorrect indentation). You need to indent def get(self): and, further indent the code the belongs in the get method. That's what
> IndentationError: expected an indented block
is trying to tell you.
I suggest getting your app running locally in dev_appserver before uploading it. That'll cut the development cycle time way down.
From the error log:
Could not initialize images API; you are likely missing the Python "PIL" module.
You need to configure PIL for your application. From the configuring third party libraries section of the documentation, add the following to your app.yaml:
libraries:
- name: PIL
version: latest
Following that you have some basic problems with your code itself. Python is sensitive to whitepace and any statement that go inside a block need to be indented, which is why you are getting this error:
File "E:\GAEGame\WebPlayer\main.py", line 22
def get(self):
^
IndentationError: expected an indented block
All statements inside a block need to be indented; the common practice is to indent by four spaces (not tabs). Here is your code, properly indented:
from google.appengine.ext.webapp import template
import webapp2
import os
class MainHandler(webapp2.RequestHandler):
def get(self):
template_values = {
'greetings': 'greetings',
}
path = os.path.join('WebPlayer', 'WebPlayer.html')
self.response.out.write(template.render(path, template_values))
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
So I've been trying to implement the Janrain Engage plugin to the application I'm working on (Google App Engine (Python 2.7)), using GAESessions as the sessions library.
Following the instructions given on the GAESessions page, I created the "gaesessions" folder (containing "__init__.py") as well as "appengine_config.py" in my application's root directory, as well as the relevant files to process the plugin.
Attempting to login via Janrain, however, threw me a 500 error and gave me this traceback in the GAE logs:
E 2013-03-25 07:06:55.535
'thread._local' object has no attribute 'current_session'
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~k-sketch-test/10.366190612177083948/rpx.py", line 56, in post
session = get_current_session()
File "/base/data/home/apps/s~k-sketch-test/10.366190612177083948/gaesessions/__init__.py", line 38, in get_current_session
return _tls.current_session
AttributeError: 'thread._local' object has no attribute 'current_session'
I have searched through the other posts regarding the "get_current_session()" issue, but they seem to be referencing 'local' instead of 'thread._local'.
Any ideas on what's going on here? Thanks in advance!
========
{root folder}/appengine_config.py
from gaesessions import SessionMiddleware
import os
COOKIE_KEY = '<hidden - was generated through os.urandom(64)>'
def webapp_add_wsgi_middleware(app):
from google.appengine.ext.appstats import recording
app = SessionMiddleware(app, cookie_key=COOKIE_KEY)
app = recording.appstats_wsgi_middleware(app)
return app
========
{root folder}/appengine_config.py Is the file appengine_config.py in the root location?
aaps/app.yaml
/main.py
/appengine_config.py
/gaesessions #folder
Can look a basic tutorial on gaesessions
This question already has an answer here:
View function did not return a response
(1 answer)
Closed 5 years ago.
i build a website with python-flask.In a page i loaded a javascript file.Here's my file structure:
/python
/static
/plugins
/themes
/default
/js
/my-js.js
/templates
/my-file.html
/venv
app.py
I have included my-js.js in my-file.html.
There is also other scripts are succesfully loaded the page(which are in the same dir with my-js.js) before this js.
When i call my file with the url /static/themes/default/js/my-js.js
it says ValueError: View function did not return a response with the traceback:
Traceback (most recent call last):
File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1361, in full_dispatch_request
response = self.make_response(rv)
File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 1439, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response
Why is it happennig? I did not wrote a route for static files such as these js.How come other js files are being loaded and this one is not?
A few things I can think of offhand. First, it looks like you're trying to pass an absolute path to my-js.js, when really you want a relative path, try the URL "static/themes/default/js/my-js.js" (no leading slash) and see if that helps.
Also, without seeing your python or Javascript, it's hard to confirm this, but are you setting the appropriate mimetype in your response object (it should be application/javascript)?
I have this piece of code which is running perfectly on localhost but throws up this obscure error on GAE:
import_string() failed for 'webapp2_extras.appengine.auth.models.User' . Possible reasons are: - missing __init__.py in a package; - package or module
My import statements:
from webapp2_extras import auth
from webapp2_extras import sessions
from webapp2_extras.auth import InvalidAuthIdError
from webapp2_extras.auth import InvalidPasswordError
Usage of auth's user model:
user = self.auth.store.user_model.create_user(username, password_raw = password, email = email)
if not user[0]: #returns a tuple with [boolean, user_info]
return 'Create user error'
else:
self.set_flash("Thank you for registering. Please login!")
self.redirect(self.auth_config['login_url'])
Full code
UPDATE (Full stack trace)
import_string() failed for 'webapp2_extras.appengine.auth.models.User'. Possible reasons are:
- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;
Original exception:
ImportError: No module named ndb
Debugged import:
- 'webapp2_extras' found in '/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/__init__.pyc'.
- 'webapp2_extras.appengine' found in '/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/appengine/__init__.pyc'.
- 'webapp2_extras.appengine.auth' found in '/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/appengine/auth/__init__.pyc'.
- 'webapp2_extras.appengine.auth.models' not found.
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~webapp-auth/1.358936463581927371/main.py", line 34, in dispatch
response = super(BaseHandler, self).dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~webapp-auth/1.358936463581927371/main.py", line 127, in post
user = self.auth.store.user_model.create_user(username, password_raw = password, email = email)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 701, in __get__
value = self.func(obj)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/auth.py", line 131, in user_model
cls = self.config['user_model'] = webapp2.import_string(cls)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1824, in import_string
return getattr(__import__(module, None, None, [obj]), obj)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/appengine/auth/models.py", line 13, in <module>
from ndb import model
Look similar to this issue which was already fixed by webapp 2.5.1
Make sure you import the latest version of webapp2, by adding those line to your app.yaml file:
libraries:
- name: webapp2
version: latest
As a workaround you can add the following lines to your application:
import sys
from google.appengine.ext import ndb
sys.modules['ndb'] = ndb