flask-admin AdminIndexView error could not build url for endpoint '/admin.static' with values ['filename', 'v']. Did you mean 'admin.static' instead? - flask

I want to customize flask-admin AdminIndexView's index view
class CustomAdminIndexView(AdminIndexView):
#expose('/')
def index(self):
return super(CustomAdminIndexView, self).index()
admin = Admin(name=app_config["APP_NAME"], template_mode="bootstrap4", endpoint="/admin",index_view=CustomAdminIndexView())
Following this code
It throws
BuildError
werkzeug.routing.exceptions.BuildError: Could not build url for endpoint '/admin.static' with values ['filename', 'v']. Did you mean 'admin.static' instead?
I only want to customize the Index view of the admin panel.

Related

Is possible to use column_formatters in edit view in Flask Admin?

Is possible to use column_formatters in edit view in Flask Admin? When I add the following code it is rendering "random" in the list form view. But when it renders the edit view this field is raw not using the column_formatters. I debugged it and indeed is using _user_formatter method just for list view.
self.column_formatters = {'model': self._user_formatter,}
def _user_formatter(self,view, context, model, name):
return "random"

Url rerouting after making an entry Django admin add page

After making an entry in django admin, django routes directly to entries page. What I want is to go to main admin page. Do I need to overwrite the admin views file or is there a way to extend it and change the url routing?
You can override the response_add() method (or response_change() for editing existing entries) inside your ModelAdmin class as follow:
def response_add(self, request, obj, post_url_continue=None):
return redirect('/admin') # Or whatever URL you want

How to use customize 404.html from decorator.py in django?

I have 4 types of user; client, officer, production_manager, service_manager. Therefore, I was creating 4 groups for each user. These users are my 4 apps in my project. In the corresponding views.py, in the methods, I'm using a decorator to give access only to a certain user, like this #group_required('client_group'). Here is my decorator method:
check_group.py
def group_required(*group_names):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(u):
if u.is_authenticated():
if bool(u.groups.filter(name__in=group_names)) | u.is_superuser:
return True
raise Http404
return user_passes_test(in_groups)
Now if a client is logged in and trying to access a certain view from url , like, officer/password, then he cannot get access of that view. I am using raise HTTP404.
This is the structure of my project:
project
|-app
|-app
|-app
|-app
|-user_group
|--|--check_group.py
|--|--404.html
But the 404 page shown by django is its default. It's not taking my 404.html.
Then in the check_group.py, I changed the code form my raise Http404 to
template = loader.get_template("404.html")
return HttpResponse(template.render())
But then I'm getting an error
officer/password doesn't contain any template 404.html
Then I put 404.html in the officer/template but still getting the same error.
I want to use my customize 404.html rather than django's built-in 404 from the decorator method, I mean, the method I wrote, check_group.py.
How can I do it?
You can do that by specifying handler in urls.py
handler404 = 'user_group.views.handler404'
Then you define view to render custom 404 html
def handler404(request):
return render(request, '404.html')
Here is the documentation on customizing error views.

How to add content to the index page using Flask-Admin

I am using flask-admin, and I want to add a dashboard to the home page. I found I can add a new page using:
admin = Admin(name='Dashboard', base_template='admin/my_master.html', template_mode='bootstrap3')
then:
admin.init_app(app)
and finally I added my_master.html, and added content. However, it is all static, how can I add custom data to that view?
I found the answer in the documentation: http://flask-admin.readthedocs.org/en/latest/api/mod_base/
It can be overridden by passing your own view class to the Admin constructor:
class MyHomeView(AdminIndexView):
#expose('/')
def index(self):
arg1 = 'Hello'
return self.render('admin/myhome.html', arg1=arg1)
admin = Admin(index_view=MyHomeView())
Also, you can change the root url from /admin to / with the following:
admin = Admin(
app,
index_view=AdminIndexView(
name='Home',
template='admin/myhome.html',
url='/'
)
)
Default values for the index page are:
If a name is not provided, ‘Home’ will be used.
If an endpoint is not provided, will default to admin Default URL route is /admin.
Automatically associates with static folder. Default template is admin/index.html
According to flask-admin documentation use this:
from flask_admin import BaseView, expose
class AnalyticsView(BaseView):
#expose('/')
def index(self):
return self.render('analytics_index.html', args=args)
admin.add_view(AnalyticsView(name='Analytics', endpoint='analytics'))

No reverse match error. How to debug?

I am trying to link the databrowse.admin widget of django that rests here :
http://127.0.0.1:8000/admin/openmaps/open_layers/
I tried to put this in a template and it returned a reverse match error. How to debug ?
A
The URL Tag you are attempting to use is specified in the Django Documentation here (for version 1.4):
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url
It's purpose is to keep the URLs in your links DRY (Don't Repeat Yourself), so that you don't have to change your link URLs between your dev, staging, production or any other server environments you may have.
The url tag takes a view or a reference to a view via a url name as its main argument, and any arguments that the view takes as second arguments. From the documentation:
{% url path.to.some_view v1 v2 %}
Where path is a package name, to is a module name and some_view is a view function. v1 and v2 are args that the view takes. It would look like so in path/to.py:
from django.http import HttpResponse
def some_view(request, v1, v2):
return HttpResponse("A response")
Furthermore, when dealing with the admin, you'll need to use the namespace admin using the URL namespace strategy, like so:
{% url admin:view_name %}
What you need to do is find the path to the view you are looking for, and create the URL using that path. To get you started, you can create a link to you your admin site index like so:
My Admin Site
These will create links for the admin logout, password change form, and app list, respectively:
Admin Logout
Change Password
The Application List
For the views on specific models within the admin, django uses the meta data on the models to construct their url names. You can do the same with your models to link to their admin pages, however, you'll need to construct their names programmatically (unless you know them). So if you had a model named Foo, you could link to its changelist view, add view, and delete view in the admin respectively by constructing their view names and using the reverse method on them:
In your view:
from django.core.urlresolvers import reverse
#...some view code...
#Get an instance of the model
bar = Foo.objects.all()[0]
prefix = "%s_%s_" % (Foo._meta.app_label, Foo._meta.module_name)
changelist_name = "%schangelist" % prefix
add_name = "%sadd" % prefix
delete_name = "%sdelete" % prefix
changelist_url = reverse(changelist_name)
add_url = reverse(add_name)
delete_url = reverse(delete_name, args=(bar.pk,)) #You need the id of the model you want to delete as an argument.
#...some more view code...
In your template
The Foo ChangeList
Add a Foo
Delete {{ bar.name }}
You'll likely have to do some digging into the guts of django or any particular extension you're using to get the exact url name that you want. If you can provide more detail about the model you're trying to access in the admin, I can provide a more specific answer.