according to the flask-admin docs I can extend the main flask-admin dashboard by creating the file templates/admin/index.html and extending admin/master.html. The HTML would look like this:
{% extends 'admin/master.html' %}
{% block body %}
<h1>HELLO</h1>
{% endblock body %}
But i can't find any information on how to extend the model CRUD pages: List, Edit and Create. I need to extend the Create and Edit User page so i can add js code to the form template.
Is there a template where i can extend just like admin/master.html example?
Just found it in flask-admin docs. I had to create templates/edit_user.html and templates/create_user.html. For list_users is also the same, theres is an example in the docs.
In edit_user.html
{% extends 'admin/model/edit.html' %}
{% block body %}
<h1>User Edit View</h1>
{{ super() }}
{% endblock %}
In create_user.html
{% extends 'admin/model/create.html' %}
{% block body %}
<h1>Create View</h1>
{{ super() }}
{% endblock %}
and then add this to the User model View:
class UserView(ModelView):
edit_template = 'edit_user.html'
create_template = 'create_user.html'
admin.add_view(UserView(User, db.session))
As for DOC, this is the default command:
admin = Admin(app, name='microblog', template_mode='bootstrap3')
Add your own CSS here /static/css/main.css:
{% block head_css %}
{{ super() }}
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css', _external=True) }}" ></link>
{% endblock %}
Related
how to completely remove the recent action panel from Django admin UI
I have done a lot of searching and all leads to how to clear the data in the panel from the database .. but what am looking to do is completely remove the panel from my admin Userinteraface .. any help on how to do this?
what I've tried :
tried to override the base_site.html for the admin and it worked for override colors and styles and other blocks but not working with the sidebar block
{% extends "admin/base.html" %}
{% load i18n static %}
{% block title %}{{ title }} | {% trans "G-Attend" %}{% endblock %}
{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{% static 'custom_admin_styles/admin_override.css' %}">
{% endblock %}
{% block branding %}
<h1 id="site-name">
<b style="color: #1f76bc;size: 80;">
G
</b> Attend </h1>
{% endblock %}
{% block sidebar %}
<div id="content-related">
</div>
{% endblock %}
{% block nav-global %}{% endblock %}
The right solution for this was like :
creating a file called index.html inside my templates/admin/ directory , then inside the HTML file I used this code :
{% extends "admin/index.html" %}
{% block sidebar %}
{% endblock %}
so I need to extend index.html instead of base_site.html
To override the Django admin side bar
You nee to extends admin/base_site.html
{% extends "admin/base_site.html" %}
{% load i18n static %}
<!-- -->
{% block sidebar %}
<div id="content-related">
Your custom side bar here.
</div>
{% endblock %}
NB : Inside your templates folder, you should have created an admin subfolder. And place the custom .html file in it.
More info Here
I have a template named base.html. As the name suggests, it is where the header and footer reside. In between these 2 elements, is a {% block content %} where the child template can extend this template and add content inside block content.
However, inside header I want the user's name to be shown. For example, {{ user.username }} but Django can't seem to recognize this when I extend this template to a child template. Is there a way I can pass in objects to the extends template? That way the logged in user's name is shown?
This is a rough example of what I'm trying to do. user.username does not show even when the user is logged in.
base.html
<header>
<h1>Hello, {{ user.username }}</h1>
</header>
{% block content %}{% endblock %}
<footer>
///Some content
</footer>
child.html
{% extends 'base.html' %}
{% block content %}
//Some content
{% endblock %}
views.py for child.html
ChildView(TemplateView):
template_name = 'child.html'
In your child template add this to the top
{% extends 'base.html' %}
This will allow you to "inherit" the context variable.
Alternatively, if you only want to pass say just the user data to the template, you could do the following in your base.html
{% include 'header.html' with my_user=user %}
This answer summarises the differences between extend and include functionality quite well.
Edit:
In response to your comments and updated question, you aren't accessing the user object correctly. To do so you must use {{ request.user }}. This is because there is a context processor that passes the user object to every template.
As an aside, if you were to explicitly send the user from the view, you could then access the user with {{ user }} as you have done. However, this is obviously quite unnecessary.
That is because the content in blocks in child templates are overriden.
base.html
{% block my_block %}
This content is overriden by child templates
{% endblock my_block %}
child.html
{% extends 'base.html' %}
{% block my_block %}
This content is shown
{% endblock my_block %}
If you want some content shown in all templates, you shouldn't put inside a block content, but directly in your base template.
base.html
{{ user.username }}
{% block my_block %}
This content is overriden by child templates
{% endblock my_block %}
So, it all comes down to how the layout of your page is done. If the header is always the same, you should not use the block tag.
If it's almost the same, but changes in details, use blocks to change the details:
header:
<h1>This doesn't change ever
{% block this_changes %}
the child themplate will provide the content
{% endblock this_changes %}</h1>
<b>User: {{ user.username }}</b>
I have a download page on a Django site that I want to serve for both users who are logged in and who aren't. Instead of having a user_download.html and login_download.html, I want to have a single download.html that conditionally extends the correct base.
However, I get an error when I use the following code.
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}
{% extends 'login_base.html' %}
{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
The error I receive is
TemplateSyntaxError at /download/
Invalid block tag: 'else'
What's wrong with the else? I tried
{% if user.is_authenticated %}
{% extends 'user_base.html' %}
{% else %}{% if AnonymousUser.is_authenticated %}
{% extends 'login_base.html' %}
{% endif %}{% endif %}
{% block content %}
<h2>Downloadable content</h2>
...
{% endblock %}
but this didn't work, either.
Thanks,
erip
The {% extends %} tag supports variables. See the doc for reference.
def my_view(request):
if request.user.is_authenicated
base_template_name = 'user_base.html'
else:
base_template_name = 'login_base.html'
# Pass base template name to the renderer
return render_to_response('your_template.html', {'base_template_name':base_template_name})
Template (please note that the value is not quoted):
{% extends base_template_name %}
...
You're getting an error because extends needs to be defined at the top of the template. extends controls template inheritance: you are basically creating a subclass from some parent class, which is why extends needs to be the first thing in the template.
Imagine writing a class, and in the __init__() you said something like
class DoesntKnowWhereToInheritFrom(object):
def __init__():
if something:
self.inherits_from(x)
else
self.inherits_from(y)
The compiler/interpreter would freak out.
The common way to do what you are trying to do here is to check for is_authenticated in the view, and then render the appropriate template.
I need to create a small side block with form(it contains only one field and button) and I want it to be included to every page except base.html
I thought about making simple view function, but maybe there are better ways to do this?
I'm using Python and Django 1.6
In general, you shouldn't use base.html directly, but because you are and because it would be a huge hassle to change it in every other template, what you can do is, in the view function that returns base.html, you can add a boolean to the context and check the boolean to determine what template you are using.
Something like this:
def view_that_uses_base.html(request):
is_base = True
return render_to_response("base.html", {"is_base":is_base}, RequestContext(request,{}))
And then in the template:
{% block sidebar %}
{% if is_base%}
{% else %}
#Your code here
{% endif %}
{% endblock sidebar %}
You must use templates to do that.
In other words, try creating $DJANGO_ROOT/templates/main.html using the following code:
<html>
<head>
</head>
<body>
{% block one_field_and_a_button %}
<input />
<button>I am everywhere</button>
{% endblock %}
{% block my_custom_content %}
{% endblock %}
</body>
<html>
Then all other templates must extend that main.html template and insert their own data.
Imagine this is $DJANGO_ROOT/templates/login.html. It will only replace "my_custom_content" and will inherit all other blocks including "one_field_and_a_button"
{% extends 'templates/main.html' %}
{% block my_custom_content %}
Hello World! This is the login
{% endblock %}
Finally, if you want to have a base.html that does not have that part of the code containing one field and a button, you can do the following.
Imagine this is $DJANGO_ROOT/templates/base.html. It will replace both "one_field_and_a_button" and "my_custom_content". However, in this case, "one_field_and_a_button" will be replaced with blank space that will not show in your html code.
{% extends 'templates/main.html' %}
{% block one_field_and_a_button %} {% endblock %}
{% block my_custom_content %}
Hello World! This is my base.html template
{% endblock %}
Hope it works for you!
You can use block tag in base.html, i think you are searching foe something like this
base.html
{% block code %}
{% include 'sidebar.html' %}
{% endblock %}
index.html
{% extends base.html %}
{% block code %}
{% endblock %}
and every other templates
just extend base html
{% extends base.html %}
i have a small problem... i want to bind my app to adminsite. my app doesnot use models.py so the only way to make my app visible in adminsite is to override the app_index.html from django's admin/templates directory. my problem is that i dont know how to override this sothat my app is in admin. i v read all the docs, but the exact way how to do this is hard to find...
here is that app_index.html:
{% extends "admin/index.html" %}
{% load i18n %}
{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs"><a href=".../">
{% trans "HOME" %}</a>
{% for app in app_list %}
{% blocktrans with app.name as name %} {{ name }} {% endblocktrans %}
{% endfor %}</div>{% endblock %}
{% endif %}
{% block sidebar %}{% endblock %}
thanks in advance...
best regards,
you don't need models.py, just register a "Model" with the admin site.
admin.site.register(class MyMenuThing(models.Model): pass)