Django URL configuration to accomodate views - django-models

I received an error 404 on my site with the following error message on my terminal console
`?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/results$' [name='results'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
?: (2_0.W001) Your URL pattern '^(?P<question_id>[0-9]+)/vote$' [name='vote'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().
`
I tried the following on the site/url.py
`from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("polls/", include('polls.urls')),
]`
and the following on my polls/url.py
`from django.conf.urls import path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
#127.0.0.1/polls
path(r'^(?P<question_id>[0-9]+)/$', views.detail, name="detail"),
#127.0.0.1/polls/1
path(r'^(?P<question_id>[0-9]+)/results$', views.results, name="results"),
#127.0.0.1/polls/1/results
path(r'^(?P<question_id>[0-9]+)/vote$', views.vote, name="vote"),
#127.0.0.1/polls/1/vote
]`
Note: polls is the name to my app
polls/views.py
`from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('Awsome guys! This is the index page of our polls application')
def detail(request, question_id):
return HttpResponse("This is the detail view of the question:%s" % question_id)
def results(request, question_id):
return HttpResponse("These are results of the question:%s" % question_id)
def vote(request, question_id):
return HttpResponse("Vote on question:%s" % question_id)`

Related

Wagtail 2.2 references API v1

I've just upgraded to Wagtail 2.2 and I'm getting an error 'wagtailadmin_api_v1 is not a registered namespace'. Part of the upgrade moved to api_v2 (which is in my INSTALLED_APPS). I did a search of the Wagtail code and found references to api_v1 in wagtail.admin.api.urls and wagtail/admin/templates/wagtailadmin/admin_base.html (there were several others in tests). I changed the references to V2, but then got the same error claiming that 'wagtail_api_v2 is not a registered namespace.
My question is whether anyone else has seen a similar problem and second, should there be any references to api_v1 in Wagtail 2.2
I was having the same error while integrating Wagtail 2.5 to an existing Django 2.2 project. My mistake was including Wagtail's urls to a namespaced urls.py.
The wagtailadmin_api_v1 is set on wagtail/admin/api/urls.py
According to the docs for url-configuration , you must include those patterns on your main urls.py.
~/projects/project_name/
manage.py
project_name/
my_app/
models.py
urls.py # Those are app-specific urls
views.py
__init__.py
...
settings.py
urls.py # This is the urls.py you should modify
wsgi.py
__init__.py
...
Pattern to be included:
from django.urls import path, re_path, include
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
urlpatterns = [
...
re_path(r'^cms/', include(wagtailadmin_urls)),
re_path(r'^documents/', include(wagtaildocs_urls)),
re_path(r'^pages/', include(wagtail_urls)),
...
]
Be sure to include those patterns on your main urls.py (it shouldn't set an app_name variable, or all the patterns will be automatically namespaced.)
Stumbled over the same issue today and this worked for me:
# api views.py
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import PagesAPIViewSet
wagtail_api_router = WagtailAPIRouter("cast:api:wagtail")
wagtail_api_router.register_endpoint("pages", PagesAPIViewSet)
# local urls.py
app_name = "api"
urlpatterns = [
...,
# wagtail api
path("wagtail/", include((views.wagtail_api_router.get_urlpatterns(), "api"), namespace="wagtail")),
]
Reverse looks now like this:
reverse("cast:api:wagtail:pages:listing")

Unittest Jinja2 and Webapp2 : template not found

I use Jinja2 with Webapp2 on a GAE project.
I have a base RequestHandler as describe in webapp2_extras.jinja2:
import webapp2
from webapp2_extras import jinja2
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {my config...}
j = jinja2.Jinja2(app, config=config)
return j
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(factory=jinja2_factory, app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
And a view handler as:
class MyHandler(BaseHandler):
def get(self):
context = {'message': 'Hello, world!'}
self.render_response('my_template.html', **context)
My templates are in the default location (templates).
The app works well on dev server, and the template is correctly rendered.
But when I try to unittest MyHandler with
import unittest
import webapp2
import webstest
class MyHandlerTest(unittest.TestCase):
def setUp(self):
application = webapp2.WSGIApplication([('/', MyHandler)])
self.testapp = webtest.TestApp(application)
def test_response(self):
response = application.get_response('/')
...
application.get_response('/my-view') raise an exception: TemplateNotFound: my_template.html.
Is there something I missed? Like a jinja2 environment or template loader configuration?
Problem origin:
Jinja2 default loader searches files in a relative ./templates/ directory. When you run your GAE application on the development server this path is relative to the root of your application. But when you run your unittests this path is relative to your unittest files.
Solution:
Not really an ideal solution, but here a trick I did to solve my problem.
I updated the jinja2 factory to add a dynamic template path, set in app config:
def jinja2_factory(app):
"""Set configuration environment for Jinja."""
config = {'template_path': app.config.get('templates_path', 'templates'),}
j = jinja2.Jinja2(app, config=config)
return j
And I set an absolute path to the templates in the setUp of my unittests:
class MyHandlerTest(unittest.TestCase):
def setUp(self):
# Set template path for loader
start = os.path.dirname(__file__)
rel_path = os.path.join(start, '../../templates') # Path to my template
abs_path = os.path.realpath(rel_path)
application.config.update({'templates_path': abs_path})

Catch All Script in App Engine Python (APP.YAML) does not work in Static Files

I have tried everything but it seems that you cannot get a catch all url...
- url: /.*
script: not_found.py
...to work on urls that are based on static directory paths. eg. I can type in www.foobar.com/asdas/asd/asd/asd/ad/sa/das/d and I can get a nice custom 404 page. But if I alter a static path url like www.foobar.com/mydir/mydir/mypage.html, I just get the horrible generic 404....
Error: Not Found
The requested URL /mydir/mydir/mypage.html was not found on this server.
... I would like to alter whatever catches the url in directory paths and writes the 404. This appears the only way to get a consistent custom 404 page in GAE Python.
Can anyone help? I have written my website from scratch and have a very limited knowledge of Python. Achieving a consistent custom 404 is the only thing I cannot seem to overcome.
EDIT/ADD : OK I've added the kind suggestion of #Lipis , and gone through getting started which which thankfully has given me a much better understanding of classes (I sadly can't vote it up yet). But! I am using a .py script found on the net and I think the NotFound class is interfering with the class that gives my index page, because now my index page is the 404 page specified by the Jinja! I have very little understanding of MainHandler so I may have to give up for now.
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import jinja2
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, {}))
class NotFound(webapp.RequestHandler):
def post(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
template = jinja_environment.get_template('404.html')
self.response.out.write(template.render(template_values))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler),('/.*', NotFound)],
debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
For better understanding I'll make some modifications on the Getting Started example which I assume that you have done it and you made some experiments with it.
It's not a good idea to have the static file for all the not found pages in the app.yaml since most likely you would like to show something more dynamic and usually the - url: /.* should be handled within your app.
In this example we are going to add a new RequestHandler for all your not found pages
import jinja2
import os
# more imports
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
class NotFound(webapp.RequestHandler):
def get(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
template = jinja_environment.get_template('not_found.html')
self.response.out.write(template.render(template_values))
application = webapp.WSGIApplication(
[('/', MainPage),
('/.*', NotFound)], # <-- This line is important
debug=True)
But in order to make the jinja2 templates work, follow carefully the modifications that you need to do in Using Templates section from the Getting Started.
The order in the URL mapping is very important so this catch all regular expression (/.*) should be always the last one, because otherwise all the other rules will be skipped.
If you want to catch all URLs, you will have to modify your main request handler in your file "not_found.py" by adding '/.*'.
For example, you can set the file "not_found.py" to:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write("Hello, MAIN!")
application = webapp.WSGIApplication(
[('/.*', MainHandler)], # <--- Add '/.*' here
debug=True)
def main():
run_wsgi_app(application)
If you navigate to www.foobar.com/asd/ad/sa/das/d or any other URL, you will see the message "Hello, MAIN!.
Hope it helps. Ask question if needed

How do I fix this TypeError?

I'm new to python and Google App Engine. I'm trying to refactor this code from Nick Johnson blog to use webapp2 and python 2.7. http://blog.notdot.net/2009/10/Blogging-on-App-Engine-part-1-Static-serving
Anyways, when I run the code below I get this error.
TypeError: get() takes exactly 2 arguments (1 given)
I think it may have something to do with the path variable not being defined, but I don't know how to define it.
import webapp2
from google.appengine.ext import webapp
from google.appengine.ext import db
class StaticContent(db.Model):
body = db.BlobProperty()
content_type = db.StringProperty(required=True)
last_modified = db.DateTimeProperty(required=True, auto_now=True)
def get(path):
return StaticContent.get_by_key_name(path)
def set(path, body, content_type, **kwargs):
content = StaticContent(
key_name=path,
body=body,
content_type=content_type,
**kwargs)
content.put()
return content
class MainHandler(webapp2.RequestHandler):
def get(self, path):
content = get(path)
if not content:
self.error(404)
return
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
The error is raised because the get method of the MainHandler class expects a path parameter.
You should add grouping to the regex in your routing definition to pass the path parameter to the get method:
app = webapp2.WSGIApplication([('(/.*)', MainHandler)],
debug=True)

Appengine serve gzipped files

I'm using AppEngine to store some pickled python objects in my app. I want to serve these to the user directly, and I'm simply using the X-AppEngine-Blobkey header to serve the files to the user with a file.pickle.gz filename. However, when I try to extract these on my computer (Mac OS) using a simple double click, the files are turned into file.pickle.gz.cpgz.
I thought it was my browser being sneaky and extracting them, but I don't think so, since
pickle.load('file.pickle.gz')
Doesn't work, and neither does
pickle.load('file.pickle.gz.cpgz')
To store the files, I use:
blobfile = files.blobstore.create(mime_type='application/gzip')
with files.open(blobfile, 'a') as f:
gz = gzip.GzipFile(fileobj=f,mode='wb')
gz.write(my_pickled_object)
gz.close()
files.finalize(blobfile)
I think I'm not understanding the way gzips work. Can someone explain?
Are you sure file.pickle.gz.cpgz is the result of your double-clicking on the file.pickle.gz file you downloaded? Usually ".cpgz" is a different kind of archive file.
I can get the code you posted to work in a development server without significant changes. Here's the code, if it helps:
#!/usr/bin/env python
from __future__ import with_statement
import gzip
import pickle
from google.appengine.api import files
from google.appengine.api import memcache
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello world! make get')
class MakeFileHandler(webapp.RequestHandler):
def get(self):
data = pickle.dumps({'a':1, 'b':True, 'c':None})
blobfile = files.blobstore.create(mime_type='application/gzip')
with files.open(blobfile, 'a') as f:
gz = gzip.GzipFile(fileobj=f,mode='wb')
gz.write(data)
gz.close()
files.finalize(blobfile)
memcache.set('filekey', files.blobstore.get_blob_key(blobfile))
self.redirect('/')
class GetFileHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
blobkey = memcache.get('filekey')
if blobkey:
self.send_blob(blobkey)
else:
self.response.out.write('No data key set back')
def main():
application = webapp.WSGIApplication([('/', MainHandler),
('/make', MakeFileHandler),
('/get', GetFileHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Click on "make", then click on "get". A file named "get.gz" is downloaded to your ~/Downloads/ folder (at least in Chrome). Double-click on it to produce a file named "get". Then:
% python
>>> import pickle
>>> pickle.load(open('get'))
{'a': 1, 'c': None, 'b': True}

Resources