Django CMS - Call a cmsplugin in a template tag - django

My problem
With Django CMS 2.3.3, when creating a Page I use cmsplugin_picture* next to a couple of other cmsplugins. In my cms template, instead of doing:
{% placholder "content" %} //calling the Django Page including all plugins...
I would like to call each cmsplugin seperately, but how would I do that?
I looked at Django tag template (filters) here and also studied Django CMS template tags here, but neither seem to suggest that possibility. I have to say I am a beginner so I might not have connected the dots...
What I try to achieve:
In my template I have a IMG tag (outside of the {% placeholder "content" %} tag) which I want to populate with an image url that I define in my Page/cmsplugin_picture. So I am looking for a placeholder tag that allows me to grab that image. In my wildest dreams I would name it:
{% show_placeholder "content" request.current_page.get_cmsplugin_picture %}
Obviously the above doesn't work, but does something like this exist?
**I have also tried cmsplugin_filer, but to me it isn't necessarely more beneficial to fix this particular problem.*
EDIT:
What I mean by Page/cmsplugin_picture -> In a Django CMS Page you can select between your installed cmsplugins to add to a Page. In my case I select cmsplugin_picture and upload an image (within that plugin). This image I want to 'call' in my Django Template. So it is a not a static url, but dynamic.

You should make a second placeholder where your img tag is (and optionally limit the types and amount of plugins using CMS_PLACEHOLDER_CONF (http://docs.django-cms.org/en/2.3.3/getting_started/configuration.html#cms-placeholder-conf).

Related

Is it possible to display the HTML provided by the admin panel app on-site?

I've built a site where I can create new posts (essays) by the admin panel. The output is visible to users. But when I place some HTML as content in the form it doesn't render itself on the page.
example:
Output on the page (with marked unrendered HTML):
I would like to know how to fix it and also, how to name the topic I want to know ( I couldn't find anything related to my problem, probably because I don't know how to express it).
Additionally, I just start to wonder if there is one more problem nested inside. How to link CSS from the static folder having this HTML mentioned above?
Django offer the autoescape template in the builtins tags
{% autoescape off %}
{{ myhtml }}
{% endautoescape %}
But your logic seems wrong, you don't need to create a new page with the doctype, just create a base template and use the block content tag to insert your article.
In your base template replace the description and title of your page by variables that will be populated by the article data.
You need to learn the basic of Django https://docs.djangoproject.com/en/4.1/ trust me you won't regret it !

Is it possible to display a specific block from a template in django?

Just like in html when you reference a section of a page with for instanceVisit the Useful Tips Section
Would there be a way to do a similar thing in django if for instance I wanted to load my page straight to the tips section? I am extending base.html to my home page that has a tips section. Right now i have a static url home i want to do the exact same but with djangos dynamic url something like {% url 'home'/#tips %}
You can just add the fragment identifier right after the URL returned by the {% url %} template tag:
home

Putting links in list_detail.object_list to list_detail.object_detail

I've started using Django and am going right to generic views. Great architecture! Well, the documents are great, but for the absolute beginner it is a bit like unix docs, where they make the most sense when you already know what you're doing. I've looked about and cannot find this specifically, which is, how do you set up an object_list template so that you can click on an entry in the rendered screen and get the object_detail?
The following is working. The reason I'm asking is to see if I am taking a reasonable route or is there some better, more Djangoish way to do this?
I've got a model which has a unicode defined so that I can identify my database entries in a human readable form. I want to click on a link in the object_list generated page to get to the object_detail page. I understand that a good way to do this is to create a system where the url for the detail looks like http://www.example.com/xxx/5/ which would call up the detail page for row 5 in the database. So, I just came up with the following, and my question is am I on the right track?
I made a template page for the list view that contains the following:
<ul>
{% for aninpatient in object_list %}
<li><a href='/inpatient-detail/{{ aninpatient.id }}/'>{{ aninpatient }}</a></li>
{% endfor %}
</ul>
Here, object_list comes from the list_detail.object_list generic view. The for loop steps through the object list object_list. In each line I create an anchor in html that references the desired href, "/inpatient-detail/nn/", where nn is the id field of each of the rows in the database table. The displayed link is the unicode string which is therefore a clickable link. I've set up templates and this works just fine.
So, am I going in the right direction? It looks like it will be straightforward to extend this to be able to put edit and delete links in the template as well.
Is there a generic view that takes advantage of the model to create the detail page? I used ModelForm helper from django.forms to make the form object, which was great for creating the input form (with automatic validation! wow that was cool!), so is there something like that for creating the detail view page?
Steve
If you're on django < 1.3 then what you are doing is basically perfect. Those generic views are quite good for quickly creating pages. If you're on django 1.3 you'll want to use the class based generic views. Once you get a handle on those they are are crazy good.
Only note I have is that you should use {% url %} tags in your templates instead of hardcoding urls. In your urls.conf file(s) define named urls like:
url('inpatient-detail/(?P<inpatient_id>\d+)/$', 'your_view', name='inpatient_detail')
and in your template (for django < 1.3):
...
In 1.3 a new url tag is available that improves life even more.

Django - Admin - Mandatory fields with ' * '

At present, Django admin will show all the mandatory fields with a bold labels. Is it possible mark with * in the label instead of bold labels?
The Django admin uses templates to render the add/edit page for a model. It is possible to replace that template with one of your own (which extends from the original template) overriding the template blocks you need to in order to make the changes you want to.
Check out the Django docs regarding overriding admin templates for more information.
It's the admin/change_form.html template which you would need to alter in some way (since this template renders the page shown when you add a new instance or edit an existing one). The existing templates already apply a required class to the appropriate labels, so I would create a new template which looks like this:
{% extends "admin/change_form.html" %}
{% block extrastyle %}
{{ block.super }}
<style type="text/css">
/* add an asterisk using CSS */
.required:after {
content: " *";
}
</style>
{% endblock %}
Apply to a Single Model
You should use a model admin class if you want this template to be used for specific models, setting the change_form_template attribute, as described in this section of the docs to the location of the template file you have created.
Apply to a Single App
If you want template to apply to models in an entire app create a templates folder inside the root of the app. Django will automatically look for templates there, so if you create a folder called admin and place a file in there called change_form.html it will automatically override the default Django template of that name (admin/change_form.html).
Project Wide
In order to apply this template project wide create a folder somewhere (not inside an app) called templates. Again place your new template in this directory at admin/change_form.html.
Next edit the template directories Django setting specifying the location of this directory in order to allow Django to find the template and override the default templates in the same way as before only project wide and not just app wide.
This is quite a complex set of things to do, especially for such a simple change and you may find it tricky if you have not worked with admin templates before (or even if you have).
Hopefully you now understand what is required to change an admin template, its actually fairly elagant (as is Django) but in my opinion not worth the effort just to change to some asterisks.

Flickr albums in django admin

I want to do the following:
Having a model (p.e. a model which handles data about photographic reports) create a section which has a preview of an specific flickr album. The URL will be provided by an URLField (until the first save the preview will not be available).
After the first save, it'll show previews of all the images inside that album, and make them selectable (through jQuery for example). Then again, when the images are selected and the object is saved (I think I can use django signals for this) it will notify a specific user telling him a selection has been made.
Is there any plugins available, or any easy way to implement this in django-admin?
Update: 22 days and no anwers... does that mean it can't be done in django-admin?
I personally can't think of any easy way to implement this in the Django admin, simply because I doubt many people who've done it have thought to open source it. I can imagine that it would be very specific to a certain user's / programmer's needs.
In any case, if you wanted to solve this issue, I'd say that your best bet would be overriding the Django admin templates in your django/contrib/admin/templates/admin folder. I believe you'd be best off by editing change_form.html.
My basic approach would be this:
Check the name of the model using opts.verbose_name. For example, if you wanted to do this processing for a model whose verbose name is "Gallery", you would do
{% ifequal opts.verbose_name "Gallery" %}
<!-- neat gallery view -->
{% else %}
<!-- regular form -->
{% endifequal %}
Make a custom template tag that will display the gallery view / form given the object_id and the type of object. This way you can replace the <!-- neat gallery view --> with a {% show_gallery object_id %}. See the Django Docs for more info on creating custom template tags. It's pretty straightforward.
Add whatever Javascript or custom stuff in your template tag template. What you choose to do is up to you.
Sorry you haven't gotten many more answers to your question. Hope this helps!