【问题标题】:Wrong class view method being called in flask app在烧瓶应用程序中调用了错误的类视图方法
【发布时间】:2020-03-04 09:53:29
【问题描述】:

我在我的烧瓶应用程序中看到了这种奇怪的行为,我为一个视图生成了一个 URL,只是让它调用另一个视图。试图追踪问题,但无法解决。

这是类视图:

@admin_blueprint.route('/admin/<string:manage_admins>', methods=['GET', 'POST'])
@login_required
@fresh_login_required
def admin_manage_admins(manage_admins, admin_id=None):
    """
    Used to managed all the registered
    admins by the super admin or the developer
    """
        # TODO finish this
    admin = Admin()
    form = AllAdminsProfile(object=admin)
    include_name = 'manage_admins'
    admins = Admin.query.all()
    if form.validate_on_submit():
        try:
            # TODO update the edited admin using 'id'
            return redirect(url_for('admin.admin', admin_id=admin_id))
        except TemplateError as err:
            print(err)
    return render_template('admin.html', page_title='Admin Dashboard', dashboard_name='Haiflytrap Dashboard', include_name=include_name, form=form, admins=admins)

# INFO Optional route that might be included in the future
# @admin_blueprint.route('/admin/<string:customers_orders>', defaults={'order_id': ''}, methods=['GET', 'POST'])
@admin_blueprint.route('/admin/<string:customers_orders>', methods=['GET'])
@login_required
def customer_orders(customer_orders):
    # order = CustomerOrder()
    # form = OrderForm(object=order)
    if customer_orders == 'customers_orders':
        try:
            orders = CustomerOrder.query.get()
            include_name = 'customers_orders'
            # if customers_orders_form.validate_on_submit():
            #     # TODO add query to update on item
            #     orders = CustomerOrder.query.get(order_id).first_or_404()
            #     customers_orders_form.populate_obj(object=orders)
            #     db.session.commit()
            #     return redirect(url_for('admin.admin'))
            return render_template('admin.html', page_title='Admin Dashboard', dashboard_name='Haiflytrap Dashboard', include_name=include_name, orders=orders)

        except TemplateError as err:
            print(err)

应该调用def customer_orders()但它调用def admin_manage_admins()的html链接:

<a href="{{ url_for('admin.customer_orders', customers_orders='customers_orders') }}" class="list-group-item list-group-item-action my-btn-secondary">Customer Orders</a>

这是使用的admin.html 模板:

{% extends 'admin_layout.html' %}
{% block content %}
  {% if include_name == 'dashboard' %}
    {% include 'dash_landing.html' %}  
  {% endif %}
  {% if include_name == 'manage_admins' %}
    {% include 'manage_admin.html' %}  
  {% endif %}

  {% if include_name == 'profile' %}
    {% include 'profile.html' %}  
  {% endif %}
  {% if include_name == 'password_update' %}
    {% include 'password_update.html' %}  
  {% endif %}
  {% if include_name == 'customers_orders' %}
    {% include 'customers_orders.html' %}  
  {% endif %}
{% endblock content %}

【问题讨论】:

    标签: python if-statement flask try-catch


    【解决方案1】:

    我说是因为你的网址是一样的 -
    他们都解析为admin/some_string
    因此,当输入 url 时,flask 只会解析第一个。

    如果您将客户订单网址更改为:
    @admin_blueprint.route('/admin/customer-orders/&lt;string:customers_orders&gt;', methods=['GET'])

    应该有所作为

    【讨论】:

    • 我认为字符串值很重要,而不仅仅是字符串!
    • 对于您的功能来说确实如此,但在这种情况下,Flask 如何区分两者?它正在寻找与该函数相关的 url,它会返回 admin/&lt;dynamic_string&gt;,因此它会转到第一个匹配的 url。
    • 有趣,谢谢你现在我知道为什么我的一些链接表现得很有趣了!
    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2020-08-01
    • 2016-05-27
    相关资源
    最近更新 更多