I am editing the template of admins
I successfully override the each model's edit page.
/myapp/template/admin/modelA/change_list_results
/myapp/template/admin/modelB/change_list_results
However how can I override the top of admin??
After login, there is a application and table lists.
I tried to override these below from /django/contrib/admin/templates/admin/ folder
/myapp/template/admin/app_index
/myapp/template/admin/index
/myapp/template/admin/base
However , still in vain.
Try to override base_site.html and include base.html using extends. I use this method to override branding.
tempaltes/admin/base_site.html
{% extends "admin/base.html" %}
{% block branding %}
<h1 id="site-name">
Name of the site
</h1>
{% endblock %}
Related
I am looking for some kind of example with flask using a required form on the home page of my site that requires the user to select an option before being allowed onto the rest of the site. The form is used for selecting a collection out of my mongo db database. I need to know what collection the user wants to use before going anywhere else on the site. Once this done I need to make sure I can use this information on my other route and views on my site.
What you want is to implement a login infrastructure.
using flask, you have a base template, where every other template is extending, what you can do is something like the following:
base.html:
{% if current_user.is_authenticated %}
<content>
{% block content %}
{% endblock %}
</content>
{% else %}
<login-form>
{% block content %}
{% endblock %}
</login-form>
{% endif %}
using this code, the content is shown only and only if the user is authenticated. in login-form HTML you should have a form to ask the credentials needed to authenticate the user and then allow them access to rest of the site.
in other template files, you continue to use the same practice:
dashboard.html:
{% extends 'base.html' %}
{% block content %}
<YOUR HTML CONTENT>
{% endblock %}
the content of dashboard.html is only shown to the user, if they are logged in(current_user.is_authenticated = True)
this is because dashboard.html is shown inside the content block of base.html witch is only shown or rendered if that condition is met.
you can use anything else instead of is_authenticated like the collection being selected or anything like that. the procedure to do it is the same.
In django admin panel, I have a requirement, how can I change the:
Django administration to the custom title?
I followed a post, but it do not give the clear steps.
I copy the django/contrib/admin/templates to my project /templates/admin, and changed the /templates/admin/base_site.html:
<h1 id="site-name">{{ site_header|default:_('Django11 administration') }}</h1>
You see, I changed the Django to Django11, but however in the browser, it do not change at all.
The other post on stackoverflow steps are punch-drunk, so who can tell me what should I do more to my requirement? is there need any configurations of my project settings? or something else?
No need to copy and change admin template
In project urls.py just put
admin.site.site_header = 'Mysite Admin Panel'
admin.site.site_title = 'Mysite Admin Panel'
Alternatively:
After copying django/contrib/admin/templates/admin/base_site.html put like this
base_site.html
{% extends "admin/base.html" %}
{% block title %}Mysite Admin Panel{% endblock %}
{% block branding %}
<h1 id="site-name">Mysite Admin Panel</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
I loaded a custom template tag note_extras.py in base.html.
base.html
<div id="wrap">
{% load note_extras %}
{% block content %}
{% endblock %}
</div><!--wrap-->
but it is not accessible at templates which is an extend of base.html ie::
home.html
{% extends "base.html" %}
{% block content %}
<div class="container">
{% create_tagmenu request.user.pk %}
</div>
{% endblock %}
it is working fine if i load note_extras in home.html ie:
{% extends "base.html" %}
{% load note_extras %}
....
In Django template language, you must load all needed template libraries in each of the templates.
I personally think it is a good idea because it makes templates more explicit (which is better than implicit). Ill give an example. Prior to Django 1.5, the default behavior for a url tag was to provide the view name in plaintext as well as all the needed parameters:
{% url path.to.view ... %}
There however was no way to provide the path to the view via a context variable:
{% with var='path.to.view' %}
{% url var ... %}
{% endwith %}
To solve that, starting with 1.3, you could import the future version of the url tag (which became the default in 1.5) by doing:
{% load url from future %}
{% url var ... %}
or
{% url 'path.to.view' ... %}
Now imagine that you would need to create a template which would extend from a base template which you did not create (e.g. one of django admin base templates). Then imagine that within the base template it would have {% load url from future %}. As a result, {% url path.to.view ... %} within your template would become invalid without any explicit explanation.
Of course this example does not matter anymore (starting with 1.5) however hopefully it illustrates a point that being explicit in templates is better than implicit which is why the currently implementation is the way it is.
If you want that a template tag is loaded in every template you want to do it in the init file of your app:
from django.template.loader import add_to_builtins
add_to_builtins('my_app.templatetags.note_extras')
In case anyone was wondering, add_to_builtins has been deprecated but one could still load a tag for all of the templates in the project via settings.TEMPLATES - supported for Django 1.9 onwards as described here:
https://stackoverflow.com/a/59719364/2447803
I am trying to get a different header in django administration. I would like to put the company's name there instead. I am trying to do it through the docs. https://docs.djangoproject.com/en/1.5/intro/tutorial02/
Near the bottem, it says to add a TEMPLATE_DIRS setting, which I did.
So, if I have:
'/LPG/firstproject/firstproject/templates',
in my TEMPLATE_DIRS
and this is where the django source file of base_html is at
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/
What exactly does it mean when it says "Now copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself"
Is this done with a command or how exactly do I do this?
Try this:
Inside your main template folder, create an admin folder. Inside it, create a file named base_site.html with the following content:
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}
{{ title }} | {% trans 'Your Site Title' %}
{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Your Site Title' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Basically, if you want to override a django admin template, you have to match the path for the template and then create your own custom template.
I want to add a javascript to the model's change_form.html template inherited from the django admin's template. I would like to show/display some of the model's attributes based on the type of login. Eg. User can be a customer or staff group member. Can anyone guide? How to use it in the {% block content %} tag of the change_form.html
You don't need content block, there is head block, you could place your js there. But that js thing that prevents some users from modifying some fields could be hacked easily.
{% block extrahead %}
{{ block.super }}
<script type="text/javascript">
$(function() {
{% if user.get_profile.is_customer %}
$('#id_of_field_block').hide();
{% endif %}
});
</script>
{% endblock extrahead %}
Also you could change change_form.html template and override content block, getting original file content as source and change fieldset template fieldset.html (or you could override only fieldset.html, I'm not sure). This template iterates over fields and there you could add some checking.