inveniosoftware / inveniosoftware/flask-breadcrumbs

More than one variable in route causes problems

Open
#41 4 comments 0 reactions 1 assignee Claimed by @jirikuncar View on GitHub
Lane: help wanted Need: tests Type: discussion
Dominant language
Python
Stars
30
Forks
24
PR merge metrics
No merged PRs in 30d

Description

I try to get to work routes which have more than one variable part with the dynamic list constructors (I can't find an example for that unfortunately). The application works itself, but the links are set wrong. in this specific case, the link on `code1` are completely wrong set.

My code to demonstrate the problem (should be executable with `python3 app.py`. I tried to strip it down to the bone to show the problem, most of the stuff is lend from the few examples):

```python
from flask import Flask, render_template_string, request

from flask_breadcrumbs import Breadcrumbs, register_breadcrumb

breadcrumbs_tpl = """


{%- for breadcrumb in breadcrumbs -%}
{{ breadcrumb.text }}
{{ '| ' if not loop.last }}
{%- endfor -%}

"""

app = Flask(__name__)
Breadcrumbs(app=app)

@app.route('/')
@register_breadcrumb(app, '.', 'Home')
def index():
return render_template_string(breadcrumbs_tpl + '
go down')

@app.route('/topic1')
@register_breadcrumb(app, '.topic1', 'Topic 1')
def topic1():
return render_template_string(breadcrumbs_tpl + '
go down')

def code1_dlc(*args, **kwargs):
code1 = request.view_args['code1']
return [{'text': code1, 'url': code1}]

@app.route('/topic1//')
@register_breadcrumb(app, '.topic1.code1', 'sub_topic1', '', dynamic_list_constructor=code1_dlc)
def sub_topic1(code1):
return render_template_string(breadcrumbs_tpl + '
go down')

@app.route('/topic1//sub_topic2')
@register_breadcrumb(app, '.topic1.code1.sub_topic2', 'sub_topic2')
def sub_topic2(code1):
return render_template_string(breadcrumbs_tpl + '
go down')

def code2_dlc(*args, **kwargs):
code2 = request.view_args['code2']
return [{'text': code2, 'url': code2}]

@app.route('/topic1//sub_topic2/')
@register_breadcrumb(app, '.topic1.code1.sub_topic2.code2', '', dynamic_list_constructor=code2_dlc)
def sub_sub_topic2(code1, code2):
return render_template_string(breadcrumbs_tpl)

if __name__ == '__main__':
app.run(debug=True)
```

Or am I doing it wrong?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.