I am new to Google App Engine and I am attempting to create a simple multi-page application (index.htm, sites.htm, topics.htm). When I run the application without file name everything works great http://localhost:9080. But when I attempt to load a specific page http://localhost:9080/index.htm or http://localhost:9080/sites.htm or http://localhost:9080/topics.htm, I receive 404 error
Here's my app.yaml
application: msa
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: static
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
My MainHandler is as below
class MainHandler(webapp2.RequestHandler):
def get(self):
path = self.request.path
temp = os.path.join(os.path.dirname(__file__),'templates/' + path)
self.response.write(temp + '<br />')
if not os.path.isfile(temp):
temp = os.path.join(os.path.dirname(__file__),'templates/index.htm')
outstr = template.render(temp, { })
self.response.out.write(outstr)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
All my files have been organized in this fashion
/
/app.yaml
/index.yaml
/main.py
/static
/static/glike.css
/templates
/templates/_base.htm
/templates/index.htm
/templates/topics.htm
/templates/sites.htm
Any guidance would be much appreciated
You have to create routes: http://webapp-improved.appspot.com/guide/routing.html#simple-routes
('/', MainHandler) will only handle: /
To handle all requests use:
app = webapp2.WSGIApplication(
[
('/.*', MainHandler),
], debug=True)
Related
Hi I have an angular 2 application which i would like to deploy to gae. So back end I am using app engine python. I am trying to do a simple post request from angular front end to app engine python. So following is my app.yaml
runtime: python27
api_version: 1
threadsafe: true
service: federation-bipedal-hominids
handlers:
- url: /.*
script: main.app
- url: (.*)/
static_files: federation-bipedal-hominids/src/app\1/index.html
upload: app
- url: (.*)
static_files: app/home\1
upload: app
main.py
import json
import webapp2
import time
# app = StaticURLParser("federation-bipedal-hominids")
def AsDict():
return {"Hello World !": "try now"}
class RestHandler(webapp2.RequestHandler):
def dispatch(self):
#time.sleep(1)
super(RestHandler, self).dispatch()
def SendJson(self, r):
self.response.headers['content-type'] = 'text/plain'
self.response.write(json.dumps(r))
class HelloWorld(RestHandler):
def post(self):
r = AsDict()
self.SendJson(r)
class QueryHandler(RestHandler):
def get(self):
r = AsDict()
self.SendJson(r)
app = webapp2.WSGIApplication([
('/HelloWorld', HelloWorld)
], debug=True)
now, how should i do it from angular front end ?
I follow this tutorial http://kay-docs.shehas.net/generic_views.html
models.py
# -*- coding: utf-8 -*-
# myapp.models
from google.appengine.ext import db
# Create your models here.
class MyModel(db.Model):
comment = db.StringProperty()
def __unicode__(self):
return self.comment
forms.py
from kay.utils.forms.modelform import ModelForm
from myapp.models import MyModel
class MyForm(ModelForm):
class Meta:
model = MyModel
urls.py
# -*- coding: utf-8 -*-
# myapp.urls
from kay.generics import crud
from myapp.forms import MyForm
from myapp.models import MyModel
class MyCRUDViewGroup(crud.CRUDViewGroup):
model = MyModel
form = MyForm
view_groups = [MyCRUDViewGroup()]
When I go to localhost:8084/mymodel/list I got
Not Found
The requested URL was not found on the server.
If you entered the URL manually please check your spelling and try
again.
Is there any suggestion what is wrong here?
UPDATE 1
app.yaml
application: xxxx
version: 4
runtime: python27
api_version: 1
threadsafe: true
inbound_services:
- mail
handlers:
- url: /favicon.ico
static_files: favicon.ico
upload: favicon.ico
mime_type: image/x-icon
- url: /_ah/mail/.+
script: kay.main.application
login: admin
- url: /media
static_dir: media
- url: /packages
static_dir: packages
- url: /_generated_media
static_dir: _generated_media
- url: /_media
static_dir: kay/media
- url: /_kay/.*
script: kay.main.application
login: admin
- url: /_ah/queue/deferred
script: kay.main.application
login: admin
- url: /_ah/test.*
script: kay.ext.testutils.gaeunit.application
login: admin
- url: /.*
script: kay.main.application
builtins:
- remote_api: on
- appstats: on
- deferred: on
libraries:
- name: jinja2
version: latest
skip_files: |
^(.*/)?(
(_backup/.*)|
(app\.yaml)|
(app\.yml)|
(index\.yaml)|
(index\.yml)|
(#.*#)|
(.*~)|
(.*\.py[co])|
(.*\.po)|
(.*\.pot)|
(\..*)|
(app\.yaml\.sample)|
(index\.yaml\.sample)|
(cron\.yaml\.sample)|
(manage\.py)|
(TODO)|
(TODO\.pdf)|
(README)|
(README\.pdf)|
(LICENSE)|
(gaema-LICENSE)|
(kay\/docs\/.*)|
(kay\/management\/.*)|
(kay\/lib\/babel\/localedata\/.*)|
)$
The error might be in your app.yaml file. That file is where all routes start.
Long time listener, first time caller.
I'm having some frustrating, seemingly inexplicable issues with Google App Engine, Jinja2, and CSS.
My templates are working, the functionality of my app works (users, blog posts, etc.), but the CSS file shows a big, fat 404 in my Chrome debugging tools and in my Google App Engine logs. Why isn't my /stylesheets/main.css loading?
Dear internet, I'd love to hear that this is just a typo. I'm sure that I'm just an idiot.
Here's my file directory:
stylesheets
main.css
templates
base.html
blog.html
front.html
login.html
newpost.html
signup.html
welcome.html
app.yaml
blogs.py
favicon.ico
index.yaml
main.py
users.py
utilities.py
Here's my YAML file:
application: hello-udacity-5681
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /.*
script: main.app
- url: /stylesheets
static_dir: stylesheets
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
Here's my main.py:
import webapp2
import os
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainHandler(Handler):
def render_front(self):
self.render("base.html")
def get(self):
self.render_front()
Here's my base.html:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<title>Blog</title>
</head>
<body>
Blog
</body>
</html>
I ran my main.css through http://jigsaw.w3.org/css-validator/ without any issues, so I won't bore you with that.
Why am I still getting a 404 for my /stylesheets/main.css ?
Your app.yaml handler section should be like this
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /stylesheets
static_dir: stylesheets
- url: /.*
script: main.app
In this case, the /stylesheets pattern will match before the /.* pattern will for the appropriate paths. For more information on URL mapping and other options you can specify in app.yaml, see the app.yaml reference.
I am trying to migrate from python to 2.7 to 2.5 but after making the required changes to main.py and app.yaml file, my site does not work
please help... what changes should i make to these to get it to work
main.py
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
app.yaml
application: cool-gadgets
version: 1
runtime: python
api_version: 1
handlers:
- url: /robots.txt
static_files: static/robots.txt
upload: static/robots.txt
- url: /favicon.ico
static_files: static/favicon.ico
upload: static/favicon.ico
- url: /gadgets/disney.xml
static_files: gadgets/disney.xml
upload: gadgets/disney.xml
- url: /gadgets/wwe.xml
static_files: gadgets/wwe.xml
upload: gadgets/wwe.xml
- url: .*
script: main.py
Changes which i made to this to migrate to 2.7
Main.py
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
app.yaml
application: cool-gadgets
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /robots.txt
static_files: static/robots.txt
upload: static/robots.txt
- url: /favicon.ico
static_files: static/favicon.ico
upload: static/favicon.ico
- url: /gadgets/disney.xml
static_files: gadgets/disney.xml
upload: gadgets/disney.xml
- url: /gadgets/wwe.xml
static_files: gadgets/wwe.xml
upload: gadgets/wwe.xml
- url: .*
script: main.application
Please don't say "does not work". That provides no useful information. You should say what you see, what error you get, etc.
In your case it seems most likely that you have an indentation error: the application at the end of main.py needs to be fully to the left, as it is a module-level variable.
I registered a Google App Engine app and I have some files below:
/index.html
/css/index.css
/js/index.js
/feedbacks/index.html
/feedbacks/css/index.css
/feedbacks/js/index.js
How should I write the app.yaml file?
Thank you!
application: appname
version: 1
runtime: python
api_version: 1
handlers:
- url: /favicon.ico
static_files: img/favicon.ico
upload: img/favicon.ico
mime_type: image/x-icon
- url: /css #your css folder
static_dir: css
- url: /scripts #your javascript folder
static_dir: scripts
- url: /img #your image folder
static_dir: img
- url: /.*
script: your_script.py
Put your first 3 files into a folder name "static"
handlers:
- url: /feedbacks/
static_dir: feedbacks
- url: /css/
static_dir: static
- url: /js/
static_dir: static
- url: /.*
script: main.py
In your main.py
class IndexHandler(webapp.RequestHandler):
def get(self):
self.redirect('static/index.html')
def main():
application = webapp.WSGIApplication([
('/index.html', IndexHandler),
], debug=True)
run_wsgi_app(application)
It's not good to write in this way,
but this will solve your problem.