Adding a link to the django admin panel - python

I have a page and I need to go to it from the admin panel. How can I specify a link in the admin panel? Maybe you can somehow add a link to the page in the header

Django's admin site provides a admin/base_site.html override point for exactly this purpose, and in fact the documentation for overriding templates talks about that template.
Make a copy of admin/base_site.html somewhere in your override hierarchy (see the linked documentation above) and modify the {% block nav-global %}{% endblock %} block to add the link you need.

Related

Define multiple templates for a predefined block wagtail CRX

I was moving a site over to wagtail and decided to use the codered extensions. The library comes with a image-gallery content-block. I want to use this but define a few templates you can choose from in the admin UI.
You usually define a template in the meta section, but I noticed a dropdown in the admin UI for a template.
How do I add a template to that dropdown? Link to the content block I want to change
I am interested in adding an HTML template and not inheriting from the content-block to change behaviour. (Unless inheriting is the only way to add a template to the dropdown.)
You could paramatise the path to the template you want to use then use an include in your block template to point to the chosen one.
For example, if you had a card block with selection for vertical or horizontal format. In your card block class you might have an property named template that uses a choice block, something like
class AlignmentChoiceBlock(ChoiceBlock):
choices=[
('blocks/flex/vertical_card.html', 'Vertical'),
('blocks/flex/horizontal_card.html', 'Horizontal')
]
Then in your block template, it just consists of:
<div class="some-block-container">
{% include value.template %}
</div>
Well, this works for Wagtail at least, not sure about codered.
The answer from Richard Allen works wagtail and is perfect for your own blocks etc. Wagtail blocks define a separate field that is used for their included components, for this you need another approach.
First you need to add the CRX_FRONTEND_TEMPLATE_BLOCKS to your django settings mysite/settings/base.py.
Then create a folder for your block templates in mysite/website/templates and create a custom template. Then add this path as a entry to the CRX_FRONTEND_TEMPLATE_BLOCKS. Entry key should be the block in lowercase. For a starter you could copy a template/html file from the codered package, found in coderedcms/blocks/
Now the template should be available from the template dropdown under the advanced menu of a crx block.
This info came from a gh issue of crx. This is e pretty recent addition and the dev mentioned that they are looking to make this easier. So this might change in the future, this worked for me on 26/01/2023.

Editing the details view of a model in flask admin

Is there a way to edit the details view model in flask-admin? I have searched the very bad docs and I couldn't find a way with which I would be able to display the model's details in a customized way!
Is there any reference to how the "details_view" should be used, assuming this is how I modify the details view?
If not, can anyone please explain to me how can I modify the way the info is displayed in that list? I have a "list of tags" column, and I wanna show tags separately based on certain criteria, I wanna apply some filters for example before showing them. How would I do that?
custom detail view can be acheived by :
1) setting the template of the modelView you are trying to customize :
class MyModelView(AdminModelView):
details_template = "admin/details.html"
2) Edit your custom template admin/details.html by totally overiding it with a whole new page. I guess you want to add additional information or custom fields most of the time, so you can start with a admin/details.html page that looks like :
{% extends 'admin/model/details.html' %}
{% block tail %}
{{ super() }}
<h1>My custom content.</h1>
{% endblock %}
By inheriting from parent template, you should have environment variable available in the template.
You may find more about available override options and such there : http://flask-admin.readthedocs.io/en/latest/api/mod_model/#flask_admin.model.BaseModelView.details_template

Conflict with django-reversion package while adding custom button in django admin

i am customizing django admin so i added a custom button to a model admin by "extending admin/change_list.html" template
{% block object-tools-items %}
//add custom button here
{% endblock %}
it looks like this
when i added a new package django-reversion for versioning of models
which created new button for recovering data but repalced custom button which i created. I figured out that this is happening because package is also extending admin/change_list.html template and overiding object-tools-items block.
and i want some thing like this. Please help.
You were already told the answer when you raised this "bug" on the reversion Github.
Just extend the reversion/change_list.html template with your own custom
template. :)
Instead of making a template with the path admin/change_list.html make an overriding template with the path reversion/change_list.html. The reason for this is that reversion does some overriding of the templates it self, so you need to make sure to play nice with those.

What is this Django template tag?

I'm working with Django and in the base template I see this: {% get_cms_html_fragment "html/footer" %}
From the surrounding syntax I can easily conclude that this is a footer include of some sort. But, it doesn't follow the traditional syntax of {% block headerexample %}{% endblock %} that is normally used in Django templates. Does anyone recognize this?
In Django you can easily create custom tags. So it can be this very project specific tag.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
This is a syntax for a template tag. FYI, block is one of the built-in template tags, get_cms_html_fragment is a custom one.
Custom template tags and filters are loaded via load built-in template tag.
See also:
Django: what does "load" do (in a template file)?

Should I use Django permissions checks in the template AND the view?

Is it advised or redundant to set permissions on the template AND the view?
Consider that any data manipulation to the DB is done through POST
if the following enough for permissions?
{% if perms.system.view_employee %}
<!-- content here -->
{% else %}
<h1>Sorry you do not have permission to access this page<h2>
{% endif %}
or should I also implement server side checking as well (is it redundant to do so or necessary)?
def my_view(request):
if not request.user.has_perm('polls.can_vote'):
return HttpResponse("You can't access this page")
else:
# do stuff
...
Checks in the template are server side.
Permissions checks in the template and in the view do not have the same purpose:
Checking permissions on the view will disallow the access to the entire page. You do this when this page, and the featured it embeds, is for APO. You handle ressources access.
Checking permissions in the template disallow parts of the template to be displayed. You do this when you want people to be able to access the page, but there are some stuff you don't want them to see on the page. You handle display.
In your particular example, you must set the permissions checks on the view to dissallow anybody to do this manipulation. Usually, if the views is accessed using POST their are little chances you want template permission checks because POST requests are actions by essence.
You usually will want template permissions checks if you:
have some parts of the page the person is not allowed to see (like sensitive data)
want to improve usability to show elements (menu, forms, etc) that are relevant only to its level of permission. Indeed it's useless to display a link to the admin in the menu if the person doesn't have the right to access the admin.

Categories

Resources