url-for

rails get app root/base url

a 夏天 提交于 2019-12-04 08:49:40
问题 In my app I have a few APIs that under api domain. Now in one of the API I want to generate a url that pointing to the main domain, say test.com/blabla... I tried to use url_for but seems the default root_url or request.host is in api domain. Url_for will make it to be api.test.com/blabla.. while I want it to be test.com/blabla... Url_for can take a parameter host: ... to set it to be test.com/, the question is how can I get the root/base url (test.com) for host? root_url or request.host are

Flask url_for URLs in Javascript

落爺英雄遲暮 提交于 2019-12-03 18:27:56
问题 What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what is the recommended way to do this in .js files? Since they are not interpreted by the template engine. What basically want to do is: // in comments.js $.post(url_for('comment.comment_reply')); Which is not possible. But naturally, I can execute that in a template: <script> $.post(url_for('comment.comment_reply')); </script> 回答1:

Query parameters with url_for?

こ雲淡風輕ζ 提交于 2019-11-30 16:54:08
url_for([:edit, @post]) is working and generating /comments/123/edit . Now I need to add a query parameter, so that instead of /comments/123/edit it is /comments/123/edit?qp=asdf I tried url_for([:edit, @post], :qp => "asdf") but no go. Use named routes. edit_post_path(@post, :qp => "asdf") You can use polymorphic_path polymorphic_path([:edit, @post], :qp => 'asdf') You can pass params to url_for . Checkout it in the sourcecode: https://github.com/rails/rails/blob/d891c19066bba3a614a27a92d55968174738e755/actionpack/lib/action_dispatch/routing/route_set.rb#L675 Michael Smart The answer from

Flask url_for URLs in Javascript

夙愿已清 提交于 2019-11-29 21:24:41
What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what is the recommended way to do this in .js files? Since they are not interpreted by the template engine. What basically want to do is: // in comments.js $.post(url_for('comment.comment_reply')); Which is not possible. But naturally, I can execute that in a template: <script> $.post(url_for('comment.comment_reply')); </script> What @dumbmatter's suggesting is pretty much considered a de facto standard way. But I thought there would be a

How do i link to images not in Static folder in flask

不打扰是莪最后的温柔 提交于 2019-11-29 18:00:44
问题 In flask, how do I serve images that are not in the static folder? I currently save the user uploaded photos on a directory that is outside the flask folder (On openshift , image currently saving in the data folder under app-root/data and the flask files are in app-root/repo/ ). In my templates, how do i serve the image files ? Using url_for , how do i reference these image files that are outside the flask folder? - data/ | -- uploads/ - repo/ | -- app/ | -- __init__.py As you can see the

Build error with variables and url_for in Flask

怎甘沉沦 提交于 2019-11-29 05:36:35
Have found one or two people on the interwebs with similar problems, but haven't seen a solution posted anywhere. I'm getting a build error from the code/template below, but can't figure out where the issue is or why it's occurring. It appears that the template isn't recognizing the function, but don't know why this would be occurring. Any help would be greatly appreciated - have been pounding my against the keyboard for two nights now. Function: @app.route('/viewproj/<proj>', methods=['GET','POST']) def viewproj(proj): ... Template Excerpt: {% for project in projects %} <li> <a href="{{ url

Rails url_for and namespaced models

守給你的承諾、 提交于 2019-11-29 02:43:37
问题 In Rails, is it possible to namespace models in modules and still get correct behavior from url_for ? For instance, here, url_for works as expected: # app/models/user.rb class User < ActiveRecord::Base end # config/routes.rb resources :users # app/views/users/index.html.haml = url_for(@user) # /users/1 Whereas after putting the User model into a module, url_for complains about an undefined method m_user_path : # app/models/m/user.rb module M class User < ActiveRecord::Base end end # config

Creating link to an url of Flask app in jinja2 template

故事扮演 提交于 2019-11-28 16:41:55
In my Flask app, I have a view which displays a post @post_blueprint.route('/post/<int:year>/<int:month>/<title>') def get_post(year,month,title): # My code To display the last 10 entries, I have following view: @post_blueprint.route('/posts/') def get_all_posts(): # My code return render_template('p.html',posts=posts) Now when I display the last 10 posts, I want to convert the title of a post into a hyperlink. Currently I have to do the following in my jinja template to achieve this: <a href="/post/{{year}}/{{month}}/{{title}}">{{title}}</a> Is there any way to avoid hard coding the url? Like

How to load a javascript or css file into a BottlePy template?

心不动则不痛 提交于 2019-11-28 04:46:36
I am trying to return a html template with BottlePy. And this works fine. But if I insert a javascript file like this in my tpl-file: <script type="text/javascript" src="js/main.js" charset="utf-8"></script> I get an 404 error. (Failed to load resource: the server responded with a status of 404 (Not Found)) Does anyone know how to fix this problem? Here is my script file: from bottle import route, run, view @route('/') @view('index') def index(): return dict() run(host='localhost', port=8080) And that is the template file, located in "./views" subfolder. <!DOCTYPE html> <html lang="de"> <head>

How to load a javascript or css file into a BottlePy template?

我的梦境 提交于 2019-11-27 05:26:24
问题 I am trying to return a html template with BottlePy. And this works fine. But if I insert a javascript file like this in my tpl-file: <script type="text/javascript" src="js/main.js" charset="utf-8"></script> I get an 404 error. (Failed to load resource: the server responded with a status of 404 (Not Found)) Does anyone know how to fix this problem? Here is my script file: from bottle import route, run, view @route('/') @view('index') def index(): return dict() run(host='localhost', port=8080)