I need to display an image in a Django form. My Django form is quite simple - a single text input field. When I initialise my Django form in a view, I would like to pass the image path as a parameter, and the form when rendered in the template displays the image. Is is possible with Django forms or would i have to display the image separately?
Thanks.
If the image is related to the form itself, not to any field in particular, then you can make a custom form class and override one of as_table(), as_ul(), as_p() methods. Or you can just use a custom template instead of leaving the form to render itself.
If it is field related then a custom widget is appropriate, as Pierre suggested.
This template responsibility to display this image in your case, I believe.
If you form need to be able to send image related information (path, url..), you'll need to create a dedicated widget.
Related
My goal is to put a custom input field in the filter list box on my Django interface. I found no way of doing this with standard tools so I want to create a new template for the filter box.
In the official Django doc on the admin interface, there's this:
It is possible to specify a custom template for rendering a list filter:
class FilterWithCustomTemplate(admin.SimpleListFilter):
template = "custom_template.html"
See the default template provided by Django (admin/filter.html) for a concrete example.
But no more details are given. How am I supposed to implement this? filter.html does not extend change_list.html and I don't know where I am supposed to put my new custom filter template (I tried in templates/admin/my_app_name with the rest of my other overriding templates but it doesn't work)
How can I create a new template for my admin filter box ?
I have this file:
templates/admin/{app_name}/{model_name}/add/change_form.html
This extends
admin/change_form.html
I added a button to this template and it is working fine WHEN adding a new instance of the model.
I want to add another button the template that appears WHEN changing already existing instance of a model. So I created this file:
templates/admin/{app_name}/{model_name}/change/change_form.html
thinking that add and change use the same template. But changes I made to this template do not seem to take place.
What template does Django use when changing the fields of already existing instance of a model?
You can specify the change_form_template in your model admin.
By default it's (in this order):
admin/APP/MODEL/change_form.html
admin/APP/change_form.html
admin/change_form.html
The model properties could be passed in GET to the template url.
I'm trying to do something a little bit different than other Django admin thumbnail questions here. I have two models that reference my Photograph model either via ForeignKey or ManyToMany and would like to be able to see the available photos in the admin interface as thumbnails instead of by their title. I've tried using an admin_thumbnail method that returns the appropriate HTML as a tag (I've used this elsewhere with success) but to no avail. The list of Photograph objects is using their __unicode__ return value to populate the list, so I tried having __unicode__ return mark_safe(the HTML string) but the list items all end up with empty strings. Any ideas? I'm bad with widgets and I'm hoping a custom widget isn't my only option here.
I have a class property that vomits some HTML:
class Task (models.Model):
def test(self):
return """<img border="0" alt="" src="/%s/%s/%s" />""" % (UPLOADS_DIR, GRAPH_DIR, "task.svg")
test.allow_tags = True
How can I easily feed the returned HTML into the model’s admin page? It works for list_display() page. I tried to make a custom widget for the test() for its model page but my widget has failed to actually display the image. I don't want to solve this problem by making a custom template for this model. Basically I'm looking for a custom form or widget that just puts any HTML that test spits out into model's admin page without modifying the output of the property.
Any thoughts?
Thanks a lot!
Eras
I know you're not keen but I think overriding the template is going to be the best way to go.
A widget is associated with a particular field but your test() method isn't. And again django forms are just concerned with fields - they're about sending data back to the server - not presenting data - that's views and templates.
It shouldn't be too messy doing this by overriding a template. If you want it really clean, you could simply extend the existing template and then add your field at the bottom. Have a look at the docs on overriding admin templates. It's quite simple to override a specific template for a specific model.
i am using mongodb for django using mongoengine .
Usually for normal models, we write forms and the after submisson of the input data(html form), we populate the django form which validates the data . If form is valid, we just save the data in django form to the model . Do I need to follow the same process for mongoengine also ? If yes, how can i validate an image field. My image field is like this :
images = ListField(ImageField())
Also, how should I ask the user to enter a list of images in the html page that can be saved into the above field . Will provide more information if necessary
Thanks.
When adding a new image to the ListField if you want to validate you can call Model.validate() to ensure its valid.
ImageFields take the actual image file - you can see some examples of their usage here:
https://github.com/MongoEngine/mongoengine/blob/master/tests/test_fields.py#L1880-1968
One caveat - I havent tried an ImageField within a ListField so let me know how it goes :)