librelist archives

« back to archive

Help on static file in blueprint

Help on static file in blueprint

From:
Duany Baro Menendez
Date:
2012-12-20 @ 20:28
Hello, I'm new in working with me like miucho flask and the framework
for its flexibility. Right now I'm just testing the blueprint module
with the following structure:
app
    module1
           static
                js
                   index.js
           templates
           app.py
   module2
           static
               js
                  index.js
           templates
           app.py
main.py

My code is as follows

main.py:

from flask import Flask, url_for
module1.config from import *
module2.app from module2 import

app = Flask (__name__)


app.register_blueprint (module1)
app.register_blueprint (module2)


app.run (debug = True, use_debugger = False, use_reloader = False)

modulo2/app.py


from flask import Blueprint, render_template, abort, url_for
jinja2 import from TemplatesNotFound


module2 = Blueprint ("module2" __name__, template_folder = "templates",
static_folder = "static")

@ modulo2.route ("/ modulo2/show")
def show ():
     try:
         render_template return ('example.html')
     TemplatesNotFound except:
         abort (404)

ejeje def ():
     return "Hello world"

modulo2/templates/ejemplo.html

<html>
<head>
<title> hehe </ title>
<script type="text/javascript"
src="{{url_for('modulo2.static',filename='js/index.js')}}"> </ script>
</ head>
<body>
EXAMPLE <h1> jajajja module2 </ h1>

</ body>
</ html>

However when accessing the url http://127.0.0.1:5000/modulo2/show not
find the file module2 index.js of how I can fix this problem

Greetings and excuse me for my English


10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS...
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION

http://www.uci.cu
http://www.facebook.com/universidad.uci
http://www.flickr.com/photos/universidad_uci

Re: [flask] Help on static file in blueprint

From:
Fazheng Li
Date:
2012-12-20 @ 20:57
you should use @modulo2.route('/show')
instead of @ modulo2.route ("/ modulo2/show")

Re: [flask] Help on static file in blueprint

From:
Duany Baro Menendez
Date:
2012-12-20 @ 21:35
The implementation of example is correct, the code of modulo2 is 



from flask import Blueprint,render_template, abort, url_for
from jinja2 import TemplatesNotFound


modulo2 = Blueprint("modulo2",__name__,template_folder="templates",
static_folder="static")

@modulo2.route("/modulo2/show")
def show():
    try:
        return render_template('ejemplo.html')
    except TemplatesNotFound:
        abort(404)

def ejeje():
    return "Hello world"

the sample contains the similar error. 

However when accessing the url http://127.0.0.1:5000/modulo2/show not
find the file module2 index.js of how I can fix this problem


El jue, 20-12-2012 a las 21:57 +0100, Fazheng Li escribió: 
> 
> 
> you should use @modulo2.route('/show')
> instead of @ modulo2.route ("/ modulo2/show")
> 
> 



10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS...
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION

http://www.uci.cu
http://www.facebook.com/universidad.uci
http://www.flickr.com/photos/universidad_uci

Re: [flask] Help on static file in blueprint

From:
Fazheng Li
Date:
2012-12-20 @ 22:49
maybe there are two ways for you to solve the problem:

1.put the js file in /static/js/ instead of /static/module/static/js
and in "example.html",use "url_for('static,filename='/js/index.js')"

2.in main.py ,
use
    app.register_blueprint (module2,url_prefix='/module2')
and in module2/app.py
use
    @ modulo2.route ("/show")
instead

I hope the second is what you want.
I don't know how to explain.
but in my opinion, one blueprint is much like one standard web application
if you use " url_prefix='/module2" when registering the blueprint,
all request url beginning with 'modules2' will be passed to the
module2/app.py
and all url for static file beginning with  "/module2/static" will be
looked for at the folder  /module2/static
but if you don't use  " url_prefix='/module2",all static file will be
looked for at /static  folder

and I hope this page can do some help to you:
http://flask.pocoo.org/docs/blueprints/#registering-blueprints