convert form field to label or text in django - django

I have a form field (dropdown) that I don't want users to edit, but only see its value as a label or text.
It simply looks like this:
{{ form.activity }}
How can I accomplish this?

Add a readonly attribute in the widget attrs.

Related

Don't render abel for id in django form

Is it possible not to render the for attribute of a label?
I want <label>Text</label> instead of <label for="id_i">Text</label> ?
you will need to manually render the form field: <label>{{field.label}}</label> will render the label as you require. See the docs for how to manually render form fields: https://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
I more flexible solution is to override id_for_label in a widget.

Django Select Widget variables

Any one know how to get the Select Widget choices in a template.
I have done theses. I am overriding admin templates and I could just change the admin form code but I don't want to for app integrity purposes.
{{field.choices}}
Yields nothing.
{%for i in field%}{{i.choices}}{%endfor%}
Yeilds ()
I need to access this var from the template.
I want to change the select field into radios.
If field is a BoundField,
{{ field.field.widget.choices }}
should get you the choices iterable.

Django MultiWidget format_output method and render method

Can anyone explain the format_output method and render method in django Multiwidget.
I have a Select box, a text field for email input and a checkbox. I have created a multiWidget but I want to have a add more feature, like if somebody clicks on the Add More button, javascript will replicate the set of fields that created by the multiWidget.
Also i want to render html like this :
<input type = "text" name=textbox_name[] />
how can I achieve this.
Thanks in advance.
Take a look at Django Formsets and related posts
dynamically add field to a form

How do I make a RadioSelect have a image as radio value?

I have the following form:
class ReviewForm(forms.ModelForm):
class Meta:
model = Review
widgets = {
'tipo' : forms.RadioSelect(),
}
But I want to use images as the values of my radio buttons, the image will vary according to the option, like this:
<input type="radio" id="id_tipo_0" value="UP" name="tipo" /><img src="/images/thumb_up.gif"/>
<input type="radio" id="id_tipo_1" value="DOWN" name="tipo" /><img src="/images/thumb_DOWN.gif"/>
I have no clues on how to achieve this.
There is a nice solution for this issue!
A ModelChoiceField has the method label_from_instance(self, obj). This method is called for every option in the ModelChoiceField.
You can overwrite ModelChoiceField.label_from_instance with:
def label_from_instance(obj):
"""
Shows an image with the label
"""
image = conditional_escape(obj.thumbnail_image.url)
title = conditional_escape(obj.title)
label = """<img src="%s" />%s""" % (image, title)
return mark_safe(label)
Note: you need to use mark_safe, otherwise the renderer will escape the img tag. Do apply the conditional_escape to the user input values (title and url).
If you want to use a regular ChoiceField you can just add HTML to the choices parameter and add mark_safe.
You can override RadioSelect (and RadioFieldRenderer) class.
OR! you can use jquery ( or something similar) to insert your img dynamically.
$(document).ready(function(){
$("#id_tipo_0").after('<img src="/images/thumb_up.gif"/>')
$("#id_tipo_1").after('<img src="/images/thumb_down.gif"/>')
});
If you want to use Django form rendering function, you'll have to use javascript to modifiy the DOM, and this will be a mess because the names of the option are rendered just after the input tag, not enclosed in any tag...
If your form does not have any other tags, go ahead, just write your input just as in your example, carefully using the Django names and values for the radio input, add a submit button, a CSRF token and that's all, you'll be able to validate your form in the view like if it was rendered via {{form.as_p}}

Django: where can I find the template that renders the fields of the admin page?

I want to change the shape of some fields showed in the admin site.
I found that the template that manage everything is change_form.html with fieldset.html but I cannot find where the fields are actually transformed in html.
Basically I want to change the field of the foreign key adding a link to another page.
Do you have any idea?
Thanks,
Giovanni
The HTML for a given field is handled by its widget in the render function. If you want to customize the look of a field you could create a custom widget which has the additional HTML you need in the render.
You can check out the render some of the built in widgets in django/forms/widgets.py (links to the Django trunk).
In fieldset.html, the code {{ field.field }} renders the field's HTML representation. to achieve what you want, you'll probably need to define your own widget. you can take a look at admin's widgets.py