我从Subdomain RailsCast获取代码
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain, request.port_string].join
end
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
end
super
end
end
class ApplicationController < ActionController::Base
include UrlHelper
end
Run Code Online (Sandbox Code Playgroud)
可以url_for
在Controllers的视图中使用修改.但我在使用ActionMailer时遇到了麻烦.
我尝试使用以下内容:
class Notifier < ActionMailer::Base
include UrlHelper
end
Run Code Online (Sandbox Code Playgroud)
但是ActionMailer视图仍然使用来自ActionDispatch :: Routing :: RouteSet的旧的未修改的url_for.
添加新url_for的最佳做法是什么?
尝试在 Flask 中使用 url_for 方法时出错。我不确定是什么原因造成的,因为我只遵循 Flask 快速入门。我是一个有一点 Python 经验的 Java 人,想学习 Flask。
这是跟踪:
Traceback (most recent call last):
File "hello.py", line 36, in <module>
print url_for(login)
File "/home/cobi/Dev/env/flask/latest/flask/helpers.py", line 259, in url_for
if endpoint[:1] == '.':
TypeError: 'function' object has no attribute '__getitem__
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的:
from flask import Flask, url_for
app = Flask(__name__)
app.debug = True
@app.route('/login/<username>')
def login(): pass
with app.test_request_context():
print url_for(login)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了 Flask 的稳定版和开发版,但错误仍然存在。任何帮助都感激不尽!谢谢你,如果我的英语不是很好,很抱歉。
我在一个名为css的文件夹中有一个文件test.css.我想为这个文件创建url.我知道,我可以使用url_for
像
url_for('static', filename="test.css")
Run Code Online (Sandbox Code Playgroud)
创建网址,static/test.css
但我不能使用像
url_for('css', filename="test.css")
Run Code Online (Sandbox Code Playgroud)
创建我感兴趣的网址 css/test.css
我怎样才能做到这一点?
如何让在表单上提交的用户输入显示在固定 URL 上?
@app.route('/', methods=['GET','POST'])
def main():
if request.method == 'POST':
msg = request.form['message']
return redirect(url_for('display_msg', msg=msg))
else:
return form
@app.route('/msg')
def display_msg(msg):
return msg
Run Code Online (Sandbox Code Playgroud)
提交表单时,我收到以下错误:
类型错误:display_msg() 缺少 1 个必需的位置参数:'msg'
此代码实际工作很好,如果我初始化msg
的全局变量,然后做global msg
在顶部main
,但它不会让我传递msg
的参数display_msg
。
这将起作用的另一种方法是,如果我更改@app.route('/msg')
为@app.route('/<string:msg>')
,但这会将 URL 更改为用户在表单上提交的任何内容,这不是我想要的。我希望 URL 被修复。
并不是每个问题,而是其他人的发现可能会有所帮助。
使用Flask建立网站时,出现
“ werkzeug.routing.BuildError:无法为端点生成URL ...”
错误。
作为开发的一部分,我创建了一个菜单列表项,其中包含许多链接为
<a href="{{ url_for('home') }}">Home</a>
... 的项目。
后来我想修改菜单,以便用一块注释掉包含原始定义的HTML行<!-- ... -->
。为了使代码正常工作并更好地理解语法和关系,我修改了单个实体,.py def name(),HTML文件名,url_for()语句等以观察其影响。
了解了与@ app.route()装饰器有关的.py函数名称(作为辅助回忆录)后,我将.py中的主页函数名称修改为“ py_home_fn”,并修改了url_for()以读取url_for(' py_home_fn')匹配给予
<a href="{{ url_for('py_home_fn') }}">Home</a>
我很困惑
'werkzeug.routing.BuildError:无法为端点'home'构建url。您是说“ py_home_fn”吗?
被报告。在控制台中查看行号时,我注意到包含错误的行在注释部分中。
在HTML带注释的部分中将url_for('home')更改为url_for('py_home_fn'),不再返回错误。
作为python / Flask nubie,这花了我更长的时间,而且到目前为止,我还没有找到任何有关此行为的在线引用,尽管那里可能有很多负载,所以想分享。
我一直在努力在IIS Web服务器上设置我的Symfony项目,但是它已经得到了很多工作.
我仍然无法获得漂亮的URL工作.当我使用我的开发环境时,url_for
生成看起来像的URL,.../frontend_dev.php/booking/create
一切正常.但是,一旦我使用prod环境url_for
生成看起来像的URL,.../booking/create
大概是因为IIS上的重写设置问题而无法工作.
我不想尝试在IIS上进行适当的重写 - 我已经填充了很长时间并且已经放弃了.如果你有一个银弹来解决我的问题,那么我很想知道,但这不是问题的真正含义.
我发现通话.../index.php/booking/create
工作正常.因此,我可以通过确保url_for
始终添加文件名来解决我的问题.但是我怎么能这样做呢?我认为它可能只是假设index.php
,所以我制作了一个副本,frontend.php
但它仍然生成了很好的URL,没有文件名.似乎如果它是开发环境,它会添加文件,但如果它是prod环境,它会隐藏它.关于如何覆盖这个的任何想法?
我在一个非常简单的应用程序中遇到了一个烧瓶url_for('')错误.
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound
base = Blueprint('main', __name__)
@base.route('/', defaults={'page':'home'})
@base.route('/<page>')
def show(page):
try:
return render_template('%s.html'%page, name='my name is')
except TemplateNotFound:
abort(404)
Run Code Online (Sandbox Code Playgroud)
以上是我的蓝图文件,这些路线工作正常,但如果我尝试做
with flask.test_request_context():
print url_for('home') #home.html or any other forms
Run Code Online (Sandbox Code Playgroud)
我刚收到这个错误:
raise BuildError(endpoint, values, method)
werkzeug.routing.BuildError: ('home', {}, None)
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我弄清楚这里发生了什么?在我的html页面中,我可以打印出静态文件的位置:
{{ url_for('static', filename='style.css') }}
Run Code Online (Sandbox Code Playgroud)
但如果我试着这样做:
{{ url_for('home') }}
Run Code Online (Sandbox Code Playgroud)
我再次得到同样的错误.任何人都有一些建议如何继续?
我正在使用url_for('module/myaction/id/3')
在修改后的管理生成器屏幕中创建链接.每当我做一个新动作并尝试链接到它时url_for ('module/action/id/3')
,我明白/module/myaction/action
.
当我尝试url_for('module/3/myaction')
我得到相同的结果.
但是当我使用时,url_for('module/myaction?id=3')
我得到了预期的结果:module/3/myaction
我的路由文件中只有默认的管理员生成的路由规则.
这种行为有望吗?
我的index.html文件包含指向同一页面上某个位置/部分的链接; 其中一个是"内容".当我在.py中使用下面的行时发送电子邮件 - 返回重定向(url_for('index'))
我想重新打开我的index.html,其中id = content我试过的部分 - url_for('index',id ="content")但不起作用.
我应该以哪种方式修改我的重定向(url_for ....行来实现这一点?请帮助.
我有一个函数来创建如下所示的新课程,如果课程是新的,则需要0个参数,如果刚刚尝试创建但是验证失败则需要5个参数.
路线:
@app.route('/courses/new')
def new_course(*args):
if len(args) == 5:
...
else:
...
Run Code Online (Sandbox Code Playgroud)
呼叫者,召集者:
...
return redirect(url_for('new_course', int(request.form['id']), course_code, semester, year, student_ids))
Run Code Online (Sandbox Code Playgroud)
我收到错误消息url_for()接受1个参数(给定6个).或者,如果我尝试:
...
return redirect(url_for('new_course', args[int(request.form['id']), course_code, semester, year, student_ids]))
Run Code Online (Sandbox Code Playgroud)
我收到错误消息new_course()需要5个参数(给定0)
我究竟做错了什么?