How to extend an ImageField in my own django admin template? - django

I would like to make a custom admin page for one of my application with django. I've created a change_form.html and fieldset.html in admin/myapp/mymodel of my template folder. I am now able to customize the page.
mymodel has an ImageField and I would like to display this image on the page. I guess this is possible because the ImageField shows a link to the image on the page.
I am trying to modify the fieldset.html but unfortunately I don't know how to access the url in order to put in an img html tag. {{field.field.field}} shows an ImageField object but how to access the current value for this field?
Thanks in advance

<img src="{{field.field.field.url}}">
On a related note, instead of having to do all that, you could use django-form-utils that will provide you a thumbnail included display and clear-able file field, for free.

The current object can be accessed in a custom django admin template with {{original}}
It solves my problem

Related

How can include a regular Django view content in a Wagtail page?

I have a website running on Django that uses Wagtail for most of the pages (that are simply text content, editable through the Wagtail Admin UI). However, I have some content that needs to be dynamically rendered from my own Django model (let's say it's a list of handouts). I'd like that content to be rendered on a Wagtail page that would also have some Wagtail models (e.g., top menu, some introductory text). What's the best way to do this? I thought about making the list of handouts an API endpoint, and rendering it with JavaScript + XHR, but it seems like there should be a good way to do it server-side.
Assuming the introductionary text is a block in a Wagtail StreamField, you could also define a Wagtail Block that links to a Django model. https://pypi.org/project/wagtail-modelchooser/ is a useful extension that provides this functionality. This makes it possible to render things from Django model instances in your Wagtail stream content.
One option would be to create a property on your Page model that retrieves whatever you are looking for from this other model
# rest of your imports
...
from handouts import Handouts
class MyPage(Page):
# rest of your page's fields
...
#property
def get_handouts(self):
handouts = Handouts.objects.all()
return handouts

CreateView not uploading image and name of the user for a post

I'm making a web app which allows users to register to the site and let them post their posts.
For taking in their post I have used the built in CBV: CreateView. In the user post I take in the name of the user using foreignkey with null=True, image, title.
The problem is: when I go to the detail page of that post it shows that there is no file associated with the image field, so I made an if statement to check if the post contains an image. But then too, it does not show the image. It shows User:None.
I don't know what is happening, I have read that CBV take the self.request automatically and there is no need to link the current user to the post.
Following are the screenshots of my code:
models.py
browser image before clicking submit button
browser image after clicking submit button (detail page of the post)
post_detail.html
views.py
If the image is in you database than you can render it this way
<img src="{{post_detail.image.url}}">
I found the answer.
I forgot to add the enctype="multipart/form-data" in the form of my createview html.
Now it's working perfectly fine.
Screenshot of userpost_form.html
Thank you all for all the support.

Flask-Admin: Adding a WYSIWYG editor to an inline model?

Pretty much as the subject has it: I have a Flask-Admin site, and would like to add TinyMCE or CKEditor to a textarea in an inline model.
There are various instructions for adding CKEditor to Flask-Admin in general, e.g. Flask-Admin and CKEditor WYSIWYG textarea integration and Getting CKEditor to work with Flask-Admin. I've followed these to create the WTForms widget and field. The inline models don't use edit.html, for pulling in the JS, but I can add it to the master template, though this is not ideal.
But the main issue is how to attach the WTForms field to the Flask-Admin model. The existing instructions have you set a form_overrides = dict(fieldname=CKTextAreaField). But that doesn't work inside inline_models. How do I accomplish this?
I had this issue a few days ago and managed to solve it by adding this to my view class.
class CustomView(ModelView):
extra_js = ['//cdn.ckeditor.com/4.6.0/standard/ckeditor.js']
inline_models=[ ( YourModel, dict(
form_overrides={
'FieldName':CKTextAreaField
}
))]
Assuming that you followed the flask admin docs tutorial for CKEditor.
inline_models consist of (model, options) pairs. The "options" object is the key here: it accepts most of the form_* attributes that ModelView has.

When overriding change_form.html template in Django Admin, the breadcrumb for its model listview is grayed out and unclickable

For some reason when overriding the change_form.html template in Django Admin, its associated listview breadcrumb is grayed out and you can't click on it. Does anyone know how to make it available again? Thank you.
It turns out that #Dev use django v1.11 and template is from django v.2.2.
In new template, django use has_view_permission to determine if user is able to access model page in admin panel. However, in django 1.11 there was no view permission and no has_view_permission method, thus it was rendered gray.

adding code for a Django admin field

I have a tinymce textarea in my django admin and I need users to be able to upload images (via AJAX) that will be linked in this textarea.
That would be accomplished by adding an "Image Upload" button (it's already working) in the top of the "Content" textarea.
What's the recommended way of doing that?
I can think on 2 solutions:
extending change_form and replacing {% for fieldset in adminform %} for the actual fields... and when it's the content field, I add this value
dynamically adding this button with javascript (find out where the content field is and add a <div> before it)
A better solution, if possible, would be to override just this specific field in the admin templates. Is that possible? Or are there better solutions?
PS: this field is not part of the DB (it just uploads one or more images, saves it to the storage and returns a link that will be included on the tinymce).
maybe you could use this
formfield_overrides from the djangodocs
if not, create a subclass of modelform and overide the form of admin with ModelAdmin.form.