Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Blueprint example app #413

Merged
merged 2 commits into from

2 participants

@dmishe

As per #404.

Though i have a couple of issues:
1. I can't mount blueprint's static under "/", would be good to show how static could be inside blueprint too (#348)
2. For some reason, if i register same blueprint under / and /pages/, / works as expected showing me index template, but /pages/ redirects back to /. Is it desired behavior?

@kennethreitz
Collaborator

I'd love to see this added. Proper use can be a little un-obvious at first glance.

@dmishe

Yo, any comments? :)

@kennethreitz
Collaborator
@kennethreitz kennethreitz merged commit 0ba758d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2012
  1. @dmishe

    Blueprint example app

    dmishe authored
  2. @dmishe

    Blueprint example tests

    dmishe authored
This page is out of date. Refresh to see the latest.
View
11 examples/blueprintexample/blueprintexample.py
@@ -0,0 +1,11 @@
+from flask import Flask
+from simple_page.simple_page import simple_page
+
+app = Flask(__name__)
+app.register_blueprint(simple_page)
+# Blueprint can be registered many times
+app.register_blueprint(simple_page, url_prefix='/pages')
+
+
+if __name__ == '__main__':
+ app.run(debug=True)
View
36 examples/blueprintexample/blueprintexample_test.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+"""
+ Blueprint Example Tests
+ ~~~~~~~~~~~~~~
+
+ Tests the Blueprint example app
+"""
+import blueprintexample
+import unittest
+
+
+class BlueprintExampleTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.app = blueprintexample.app.test_client()
+
+ def test_urls(self):
+ r = self.app.get('/')
+ self.assertEquals(r.status_code, 200)
+
+ r = self.app.get('/hello')
+ self.assertEquals(r.status_code, 200)
+
+ r = self.app.get('/world')
+ self.assertEquals(r.status_code, 200)
+
+ #second blueprint instance
+ r = self.app.get('/pages/hello')
+ self.assertEquals(r.status_code, 200)
+
+ r = self.app.get('/pages/world')
+ self.assertEquals(r.status_code, 200)
+
+
+if __name__ == '__main__':
+ unittest.main()
View
0  examples/blueprintexample/simple_page/__init__.py
No changes.
View
13 examples/blueprintexample/simple_page/simple_page.py
@@ -0,0 +1,13 @@
+from flask import Blueprint, render_template, abort
+from jinja2 import TemplateNotFound
+
+simple_page = Blueprint('simple_page', __name__,
+ template_folder='templates')
+
+@simple_page.route('/', defaults={'page': 'index'})
+@simple_page.route('/<page>')
+def show(page):
+ try:
+ return render_template('pages/%s.html' % page)
+ except TemplateNotFound:
+ abort(404)
View
5 examples/blueprintexample/simple_page/templates/pages/hello.html
@@ -0,0 +1,5 @@
+{% extends "pages/layout.html" %}
+
+{% block body %}
+ Hello
+{% endblock %}
View
5 examples/blueprintexample/simple_page/templates/pages/index.html
@@ -0,0 +1,5 @@
+{% extends "pages/layout.html" %}
+
+{% block body %}
+ Blueprint example page
+{% endblock %}
View
25 examples/blueprintexample/simple_page/templates/pages/layout.html
@@ -0,0 +1,25 @@
+<!doctype html>
+<title>Simple Page Blueprint</title>
+<div class=page>
+ <h1>This is blueprint example</h1>
+ <p>
+ A simple page blueprint is registered under / and /pages<br/>
+ you can access it using this urls:
+ <ul>
+ <li><a href="{{ url_for('simple_page.show', page='hello') }}">/hello</a></li>
+ <li><a href="{{ url_for('simple_page.show', page='world') }}">/world</a></li>
+ </ul>
+ </p>
+ <p>
+ Also you can register the same blueprint under another path
+ <ul>
+ <li><a href="/pages/hello">/pages/hello</a></li>
+ <li><a href="/pages/world">/pages/world</a></li>
+ </ul>
+ </p>
+
+
+
+ {% block body %}
+ {% endblock %}
+</div>
View
5 examples/blueprintexample/simple_page/templates/pages/world.html
@@ -0,0 +1,5 @@
+{% extends "pages/layout.html" %}
+
+{% block body %}
+ World
+{% endblock %}
Something went wrong with that request. Please try again.