Flask app.route(" / authors / add" button无法为端点构建网址' / authors / add'。您的意思是'作者'相反?

时间:2018-04-15 00:45:38

标签: python flask routes

我添加了一个按钮,可以在我的authors.html页面中添加作者,但按钮是 url导致此错误的问题

werkzeug.routing.BuildError: Could not build url for endpoint '/authors/add'. Did you mean 'authors' instead?

authors.html

<a href="{{ url_for('/authors/add')}}">  <button type="button" id="addAuthorButton" class="btn-primary" name="button">Add Author</button></a>

routes.py

  @app.route("/authors")
  def authors():
  ....

  @app.route("/authors/add",methods=['GET','POST'])
  def addAuthor():
form =createAuthorForm()
return render_template('addAuthor.html',form=form)

1 个答案:

答案 0 :(得分:3)

url_for()将函数名称作为参数,而不是url。试试这个:

[I 01:37:29.009 LabApp] The port 8888 is already in use, trying another port.
[I 01:37:29.010 LabApp] The port 8889 is already in use, trying another port.
[I 01:37:29.010 LabApp] The port 8890 is already in use, trying another port.
[I 01:37:29.011 LabApp] The port 8891 is already in use, trying another port.
[I 01:37:29.011 LabApp] The port 8892 is already in use, trying another port.
[I 01:37:29.031 LabApp] JupyterLab beta preview extension loaded from C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab
[I 01:37:29.031 LabApp] JupyterLab application directory is C:\Users\Utilizador\Anaconda3\share\jupyter\lab
[W 01:37:29.036 LabApp] Error loading server extension jupyterlab
Traceback (most recent call last):
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\commands.py", line 321, in __init__
self._run(['node', 'node-version-check.js'], cwd=HERE, quiet=True)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\commands.py", line 1165, in _run
proc = Process(cmd, **kwargs)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\process.py", line 73, in __init__
self.proc = self._create_process(cwd=cwd, env=env)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\process.py", line 131, in _create_process
cmd[0] = which(cmd[0], kwargs.get('env'))
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\jlpmapp.py", line 59, in which
raise ValueError(msg)
ValueError: Please install nodejs 5+ and npm before continuing installation. nodejs may be installed using conda or directly from the nodejs website.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\notebook\notebookapp.py", line 1454, in init_server_extensions
func(self)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\extension.py", line 111, in load_jupyter_server_extension
info = get_app_info(app_dir)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\commands.py", line 244, in get_app_info
handler = _AppHandler(app_dir, logger)
File "C:\Users\Utilizador\Anaconda3\lib\site-packages\jupyterlab\commands.py", line 324, in __init__
raise ValueError(msg)
ValueError: Please install nodejs 5+ and npm before continuing installation. nodejs may be installed using conda or directly from the nodejs website.
[I 01:37:29.143 LabApp] Serving notebooks from local directory: C:\Users\Utilizador
[I 01:37:29.143 LabApp] 0 active kernels
[I 01:37:29.143 LabApp] The Jupyter Notebook is running at:
[I 01:37:29.143 LabApp] http://localhost:8821/?token=3b91702d19818c1d66dc2965898bf2b91a24c5234e0cbd05
[I 01:37:29.143 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 01:37:29.145 LabApp] 
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8821/?token=3b91702d19818c1d66dc2965898bf2b91a24c5234e0cbd05
[I 01:37:29.244 LabApp] Accepting one-time-token-authenticated connection from ::1
[W 01:37:29.307 LabApp] 404 GET /lab?token=f8eabc16b28360868e8c304de5929a0d3b91126814057e49 (::1) 64.17ms referer=None