{"action":"created","issue":{"url":"https://api.github.com/repos/flask-admin/flask-admin/issues/1313","repository_url":"https://api.github.com/repos/flask-admin/flask-admin","labels_url":"https://api.github.com/repos/flask-admin/flask-admin/issues/1313/labels{/name}","comments_url":"https://api.github.com/repos/flask-admin/flask-admin/issues/1313/comments","events_url":"https://api.github.com/repos/flask-admin/flask-admin/issues/1313/events","html_url":"https://github.com/flask-admin/flask-admin/issues/1313","id":165944585,"number":1313,"title":"BaseView.get_url is not respected sometimes","user":{"login":"MarSoft","id":171243,"avatar_url":"https://avatars.githubusercontent.com/u/171243?v=3","gravatar_id":"","url":"https://api.github.com/users/MarSoft","html_url":"https://github.com/MarSoft","followers_url":"https://api.github.com/users/MarSoft/followers","following_url":"https://api.github.com/users/MarSoft/following{/other_user}","gists_url":"https://api.github.com/users/MarSoft/gists{/gist_id}","starred_url":"https://api.github.com/users/MarSoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarSoft/subscriptions","organizations_url":"https://api.github.com/users/MarSoft/orgs","repos_url":"https://api.github.com/users/MarSoft/repos","events_url":"https://api.github.com/users/MarSoft/events{/privacy}","received_events_url":"https://api.github.com/users/MarSoft/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-07-16T19:45:54Z","updated_at":"2016-08-02T12:54:37Z","closed_at":null,"body":"According to docstring, `BaseView.get_url` method can be used to persist query string arguments.\r\nHere is my usecase: I have two views - `LoginView` and RegisterView`, and they both accept `next` argument. I overriden a `get_url` method for both of them to check it there is a `next` parameter in `request.values` and add it to `kwargs`. This works; but here is a problem:\r\n\r\nWhen a view is referenced from the menu, it is wrapped in `MenuView` class. This class by default will cache generated URLs, with no respect to current request.\r\nAs a result, urls from the first request made after app restart are cached. After that, my custom `get_url` is never called for menu links.\r\n\r\nHere is my (partial) solution: I have subclassed `Admin` class and overriden its `_add_view_to_menu` method, so that it checks if the view in question has a mixin which provides custom `get_url` implementation, and if so then it creates `MenuView` instance itself with `cache=False` added. Else it will just call superclass' implementation.\r\n\r\n```python\r\nclass MyAdmin(Admin):\r\n    def _add_view_to_menu(self, view):\r\n        if not isinstance(view, RetainNextParamMixin):\r\n            return super()._add_view_to_menu(view)\r\n\r\n        self.add_menu_item(MenuView(view.name, view, cache=False), view.category)\r\n\r\nclass RetainNextParamMixin:\r\n    def get_url(self, endpoint, **kwargs):\r\n        if endpoint != 'admin.static':\r\n            kwargs.setdefault(request.values.get('next'))\r\n        return super().get_url(endpoint, **kwargs)\r\n```\r\n\r\nProbably we can add some parameter like `no_cache_url` to BaseView class, to be used in such situations?"},"comment":{"url":"https://api.github.com/repos/flask-admin/flask-admin/issues/comments/236895055","html_url":"https://github.com/flask-admin/flask-admin/issues/1313#issuecomment-236895055","issue_url":"https://api.github.com/repos/flask-admin/flask-admin/issues/1313","id":236895055,"user":{"login":"MarSoft","id":171243,"avatar_url":"https://avatars.githubusercontent.com/u/171243?v=3","gravatar_id":"","url":"https://api.github.com/users/MarSoft","html_url":"https://github.com/MarSoft","followers_url":"https://api.github.com/users/MarSoft/followers","following_url":"https://api.github.com/users/MarSoft/following{/other_user}","gists_url":"https://api.github.com/users/MarSoft/gists{/gist_id}","starred_url":"https://api.github.com/users/MarSoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarSoft/subscriptions","organizations_url":"https://api.github.com/users/MarSoft/orgs","repos_url":"https://api.github.com/users/MarSoft/repos","events_url":"https://api.github.com/users/MarSoft/events{/privacy}","received_events_url":"https://api.github.com/users/MarSoft/received_events","type":"User","site_admin":false},"created_at":"2016-08-02T12:54:37Z","updated_at":"2016-08-02T12:54:37Z","body":"@mrjoes, yes, this is already supported - I do use this support in my workaround.\r\nThe problem is that by default (without subclassing `Admin`) we cannot influence how `MenuItem` is being created in `Admin.add_view`, which class is used or which parameters are passed. So in order to pass that `cache=False`, I had to subclass `Admin`.\r\nYour method with inheriting from `MenuView` class will not work for the same reason: default implementation of `Admin` always uses `MenuView` class itself.\r\n\r\nWhat I would like to suggest is to expose that `cache` parameter for `MenuView`, e.g. in the following way:\r\n\r\n```python\r\nclass Admin:\r\n    def _add_view_to_menu(self, view):\r\n        self.add_menu_item(MenuView(view.name, view, cache=view.url_cached), view.category)\r\n\r\nclass BaseView:\r\n    url_cached = True\r\n    \"\"\"\r\n    By default, URL returned by `get_url` will be cached in menus.\r\n    Set this to False to force url regenerating each time.\r\n    \"\"\"\r\n```"}}
{"actor":{"display_login":"MarSoft"}}