I am struggling with a requirement now, I want to add an image to choice field label and I really dont have a clue how to do it. I am using django form wizard to render the form.
Here is the image which shows what I want to achieve :
And here is what I have got right now ( to make the radio buttons inline, I know it could be achieved through css):
Here is the forms.py:
from django import forms
from django.utils.translation import gettext as _
CHOICES=[('0','Pay by card'), ('1','Invoice')]
class PaymentForm(forms.Form):
title = 'payment'
payment_method = forms.ChoiceField(label = _("Payment Options"), choices=CHOICES, widget=forms.RadioSelect(), required = True)
I am rendering using wizard form:
{{ wizard.form.payment_method.label_tag }}
{{ wizard.form.payment_method|safe }}
{{ wizard.form.payment.errors}}
Anyone has any suggestion for this apart from custom widget?
Without a widget:
from django.utils.safestring import mark_safe
CHOICES=[('0', mark_safe('Pay by card <img src="by_card.jpg"/>')),
('1', mark_safe('Invoice <img src="no_card.jpg"/>'))
]
Credit: setting help_text for each choice in a RadioSelect
1) Shortly (but i'm not sure) call a function where 'Pay by card' and return all <img>... that you need.
2) You can make somthing like #Gahbu said
3)Long [Better, i think, but untested :( ]:
Make a renderer:
from myapp.my_widgets import CardsRadioFieldRenderer
CARD_CHOICE = '0'
CHOICES=[(CARD_CHOICE,'Pay by card'), ('1','Invoice')]
class PaymentForm(forms.Form):
title = 'payment'
payment_method = forms.ChoiceField(label = _("Payment Options"), choices=CHOICES,widget=forms.RadioSelect(renderer=CardsRadioFieldRenderer), required = True)
# myapp/my_widgets.py
class CardRadioInput(RadioInput):
def __init__(self, name, value, attrs, choice, index):
self.name, self.value = name, value
self.attrs = attrs
choice_value = force_text(choice[0])
self.choice_value = choice_value
if choice_value == CARD_CHOICE:
choice_label = force_text(self.get_html_for_card_choice())
else:
choice_label = force_text(choice[1])
self.choice_label = choice_label
self.index = index
def get_html_for_card_choice(self):
#some logic to get the images tags (<img ...> <img ...>)
return text
class CardsRadioFieldRenderer(RadioFieldRenderer):
def __getitem__(self, idx):
choice = self.choices[idx] # Let the IndexError propogate
return CardRadioInput(self.name, self.value, self.attrs.copy(), choice, idx)
In your template do something like :
{% for choice in wizard.form.payment_method.choices %}
{{ choice.0 }} {# value #} {{ choice.1 }} {# value #}
{% if choice.0 == PAYMENT_BY_PAYPAL %}
...
{% endif %}
{% endfor %}
You can also write :
{% for key, value in wizard.form.payment_method.choices %}
{% if key == PAYMENT_BY_PAYPAL %}
...
{% endif %}
{% endfor %}
If you want to tweak it from the template, you need to use the "safe" which tells Jinja not to escape it. like below
{{ some_object.some_property|safe }}
Yet if you want to use it inside your python code while defining the form/modelform just use mark_safe as below.
mark_up = 'this is a link'
choices = list()
choices.append(('some_id', mark_safe(mark_up)))
self.fields['some_field'].choices = choices
Related
views.py:
from django.views import generic
from .models import Servico
class ServicoView(generic.DetailView):
model = Servico
context_object_name = 'servico'
template_name = 'servico.html'
models.py:
from djongo import models
class PublicoAlvo(models.Model):
def __str__(self):
return ''
alvo1 = models.CharField(max_length = 126)
alvo2 = models.CharField(max_length = 126, blank = True, default = '')
class Meta:
abstract = True
class Servico(models.Model):
t_id = models.CharField(primary_key = True, unique = True, max_length = 252)
alvos = models.EmbeddedField(
model_container = PublicoAlvo
)
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('servicos/<slug:pk>/', views.ServicoView.as_view(), name = 'servico')
]
I think these are the relevant files in my Django app folder. Back to the question, how can I iterate over the values that are going to be stored in servico.alvos in my template? If I want to show t_id, I just use {{ servico.t_id }} and it works fine. I could write something like:
<HTML1>
{{ servico.alvos.alvo1 }}
<HTML2>
<HTML1>
{{ servico.alvos.alvo2 }}
<HTML2>
And that would show the values that I want, but would make things uglier, since I would have to write a lot of repeated standard HTML (that I indicated as and ) to format each value inside servico.alvos, and more limited (imagine if I decide to change the model and add more 6 values in the PublicoAlvo class). I tried the following:
{% for alvo in servico.alvos.all %}
<HTML1>
{{ alvo }}
<HTML2>
{% endfor %}
and
{% for alvo in servico.alvos.items %}
<HTML1>
{{ alvo }}
<HTML2>
{% endfor %}
But I get nothing printed. When I try:
{% for alvo in servico.alvos %}
<HTML1>
{{ alvo }}
<HTML2>
{% endfor %}
I get 'PublicoAlvo' object is not iterable
Is there a way to get what I want using a loop in my template or changing something in my models.py?
Try
{{ servico.t_id }}
{{ servico.alvos }}
Then in your models.py
class PublicoAlvo(models.Model):
def __str__(self):
# Option 1: List all <alvo> fields
return ", ".join(
[
self.alvo1,
self.alvo2,
]
)
# Option 2: If you don't like manually listing each <alvo> field
# return ", ".join(
# [
# getattr(self, f"alvo{index}") for index in range(1, 3)
# ]
# )
...
This might give something like
The value for alvo1, While for alvo2 is this one
Update
You could also try
{% for key, value in servico.alvos.items %}
{% if key|slice:":4" == "alvo" %}
{{ value }}<br>
{% endif %}
{% endfor %}
This might show something like
The value for alvo1
While for alvo2 is this one
Based on the first answer of Niel Godfrey Ponciano, I was able to solve the problem.
models.py:
from djongo import models
class PublicoAlvo(models.Model):
def __str__(self):
return ''
def list(self):
return [value for key, value in self.__dict__.items() if not key.startswith('_')]
alvo1 = models.CharField(max_length = 126)
alvo2 = models.CharField(max_length = 126, blank = True, default = '')
class Meta:
abstract = True
class Servico(models.Model):
t_id = models.CharField(primary_key = True, unique = True, max_length = 252)
alvos = models.EmbeddedField(
model_container = PublicoAlvo
)
And then I can iterate over servico.alvos.list using a for in the template just by adding the list method that returns the relevant fields (variables) values in my class.
I have a base template file (shared.html) contains Header and Footer, using on every page. I have some dynamic values from database in shared.html (e.g: Phone number, address, logos) and showing perfectly on index view page, but not showing on any of the generic views page.
Please guide me how to do this to show all the dynamic values on every generic view page.
Index view:
def index(request):
# Display all the Dynamic values form models
num = TopBar.objects.get()
addressISD = AddressIslamabad.objects.all()
addressRWP = AddressRawalpindi.objects.all()
alt = Logo.objects.all()
YLP7Text = WhyLP7Text.objects.all()
PMBG = BGimages.objects.all()
lp7Features = LP7features.objects.all()
locate = Locations.objects.all()
events = Events.objects.all()
memberLogo = LP7MembersLogo.objects.all()
testi = LP7Testimonials.objects.all()
promo = Promotions.objects.all()
c = context = ({
'topBarNumber': num,
'addressISD': addressISD,
'addressRWP': addressRWP,
'altText': alt,
'YLP7Text': YLP7Text,
'BG': PMBG,
'featuresLP7': lp7Features,
'LC': locate,
'evt': events,
'memLogo': memberLogo,
'testi': testi,
'promo': promo
})
# Render the HTML template index.html
return render(request, 'index.html', c )
Generic View:
# Display the detail and generic views
class EventListView(generic.ListView):
model = Events
class EventDetailView(generic.DetailView):
model = Events
class PromotionListView(generic.ListView):
model = Promotions
class PromotionDetailView(generic.DetailView):
model = Promotions
I think you can write a custom context processor for this. For example:
def custom_context_processor(request):
# Display all the Dynamic values form models
num = TopBar.objects.get()
addressISD = AddressIslamabad.objects.all()
addressRWP = AddressRawalpindi.objects.all()
alt = Logo.objects.all()
YLP7Text = WhyLP7Text.objects.all()
PMBG = BGimages.objects.all()
lp7Features = LP7features.objects.all()
locate = Locations.objects.all()
events = Events.objects.all()
memberLogo = LP7MembersLogo.objects.all()
testi = LP7Testimonials.objects.all()
promo = Promotions.objects.all()
context = {
'topBarNumber': num,
'addressISD': addressISD,
'addressRWP': addressRWP,
'altText': alt,
'YLP7Text': YLP7Text,
'BG': PMBG,
'featuresLP7': lp7Features,
'LC': locate,
'evt': events,
'memLogo': memberLogo,
'testi': testi,
'promo': promo
}
return { 'common_content': context }
And then add it to CONTEXT_PROCESSORS like this:
TEMPLATES = [
{
# ...
'OPTIONS': {
'context_processors': [
'path.to.custom_context_processor',
# rest of the context processors
],
},
},
]
And use this context processor in header and footer like this:
{% for key, value in common_content.items %}
{{ key }} {{ value }}
{% endfor %}
Finally, as it is making a lot of DB query, you can use template fragment caching to reduce DB hits.
In your generic view,
class PromotionDetailView(generic.DetailView):
model = Promotions
template_name = 'promotions/promotions.html' #<app_name>/<model>_<viewtype>.html
context_object_name = 'Promotions'
in your promotions.html do this
{% extends 'blog/base.html'%}
{% block content %}
{% for promotion in Promotions%}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ promotion.logos.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-post' promotion.Addresses %}">{{ promotion.Name }}</a>
<small class="text-muted">{{ promotion.Date_posted|date:"d F, Y" }}</small>
</div>
<p class="article-content">{{ promotion.Phone_Number}}</p>
</div>
</article>
{% endfor %}
{% endblock content %}
In GenericViews you can override the get_context_data method in order to set the context that you need in your templates.
You can do something like this:
class EventListView(generic.ListView):
model = Events
# We could explicitly tell the view which template
# to use by adding a template_name attribute to the view,
# but in the absence of an explicit template Django will infer one
# from the object’s name
# template_name = 'path/template.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Update context with data needed
context['foo'] = 'bar'
context['foo_2'] = 'bar_2'
return context
In your template you will then be able to access the context as:
{% for object in object_list %}
{{ object.foo }}
{{ object.foo_2 }}
{% endfor %}
For DetailViews you can override the same get_context_data method, but will not need a loop in the template.
You can read more about this in the documentation, these examples are quite elaborate.
My Django Model:
class myModel(models.Model):
myIntA = models.IntegerField(default=0)
My View:
myModelList = myModel.objects.all()
for i in range(len(myModelList)):
myModelList[i].myIntB = i
return render(
request,
'myApp/myTemplate.html',
Context(
{
"myModels": myModelList,
}
)
)
Is the above legal? You can see that I added a variable myIntB to each myModel object.
However when I try to print myIntB in the template below, nothing shows up.
How can I access myIntB from the template? It is not a field I have defined for this model, nor do I want it to be. I just want myModel to be augmented with this extra variable during rendering of this particular template.
My Template:
{% for currModel in myModels %}
{{currModel.myIntA}}<br/>
{{currModel.myIntB}}<br/>
{% endfor %}
Replace following line:
myModelList = myModel.objects.all()
with:
myModelList = list(myModel.objects.all())
Otherwise, new queries are performed everytime you access myModelList[i]; you lose the change you made.
Alternatively, what you want is simply counter, you can use forloop.counter or forloop.counter0 in the template.
No that won't do what you are thinking; try this instead:
enriched_models = []
myModelList = myModel.objects.all()
for i in myModelList:
enriched_models.append((i, 'foo'))
return render(request, 'myApp/myTemplate.html', {"myModels": enriched_models})
Then in your template:
{% for currModel,extra in myModels %}
{{ currModel.myIntA }}<br/>
{{ extra }}<br/>
{% endfor %}
I need to retrieve an optional number saved in DB , to a custom template tag i made . which to retrieve , a variable ( a photo ID ) included in this Gallery . within the gallery loop .
{% get_latest_photo {{photo.id}} %}
How to accomplish that ?!
P.s : I know that can be done with inclusion tag , but in the present time how to make it fix this one !
Edit the template html file :
{% for album in albumslist %}
{% get_latest_photo photo.id %}
{% for photo in recent_photos %}
<img src='{% thumbnail photo.image 200x80 crop,upscale %}' alt='{{ photo.title }}' />
{% endfor %}
{{ album.title }}
{% endfor %}
templatetag
from django.template import Library, Node
from akari.main.models import *
from django.db.models import get_model
register = Library()
class LatestPhotoNode(Node):
def __init__(self, num):
self.num = num
def render(self, context):
photo = Photo.objects.filter(akar=self.num)[:1]
context['recent_photos'] = photo
return ''
def get_latest_photo(parser, token):
bits = token.contents.split()
return LatestPhotoNode(bits[1])
get_latest_photo = register.tag(get_latest_photo)
P.s Its working very well when i replace album.id (in {% get_latest_photo photo.id %} ) with a number which acts as an album id and retrieve the photo from .
Regards
H. M.
You don't put the brackets around variables when you use them in template tags.
{% get_latest_photo photo.id %}
To evaluate correctly the num variable I think you should modify your LatestPhotoNode class like this:
class LatestPhotoNode(Node):
def __init__(self, num):
self.num = template.Variable(num)
def render(self, context):
num = self.variable.resolve(self.num)
photo = Photo.objects.filter(akar=num)[:1]
context['recent_photos'] = photo
return ''
Are you sure your template tag is written properly? For example, you need to use Variable.resolve to properly get the values of variables: Passing Template Variables to the Tag
I had the same problem problem and after reading the docs, I solved it using this
class LatestPhotoNode(Node):
def __init__(self, num):
self.num = template.Variable(num)
def render(self, context):
num = self.num.resolve(context)
photo = Photo.objects.filter(akar=num)[:1]
context['recent_photos'] = photo
return ''
If you are trying to render multiple variables, using json.dumps is very useful.
I have a model:
from django.db import models
CHOICES = (
('s', 'Glorious spam'),
('e', 'Fabulous eggs'),
)
class MealOrder(models.Model):
meal = models.CharField(max_length=8, choices=CHOICES)
I have a form:
from django.forms import ModelForm
class MealOrderForm(ModelForm):
class Meta:
model = MealOrder
And I want to use formtools.preview. The default template prints the short version of the choice ('e' instead of 'Fabulous eggs'), becuase it uses
{% for field in form %}
<tr>
<th>{{ field.label }}:</th>
<td>{{ field.data }}</td>
</tr>
{% endfor %}.
I'd like a template as general as the mentioned, but printing 'Fabulous eggs' instead.
[as I had doubts where's the real question, I bolded it for all of us :)]
I know how to get the verbose version of a choice in a way that is itself ugly:
{{ form.meal.field.choices.1.1 }}
The real pain is I need to get the selected choice, and the only way coming to my mind is iterating through choices and checking {% ifequals currentChoice.0 choiceField.data %}, which is even uglier.
Can it be done easily? Or it needs some template-tag programming? Shouldn't that be available in django already?
In Django templates you can use the "get_FOO_display()" method, that will return the readable alias for the field, where 'FOO' is the name of the field.
Note: in case the standard FormPreview templates are not using it, then you can always provide your own templates for that form, which will contain something like {{ form.get_meal_display }}.
The best solution for your problem is to use helper functions.
If the choices are stored in the variable CHOICES and the model field storing the selected choice is 'choices' then you can directly use
{{ x.get_choices_display }}
in your template. Here, x is the model instance.
Hope it helps.
My apologies if this answer is redundant with any listed above, but it appears this one hasn't been offered yet, and it seems fairly clean. Here's how I've solved this:
from django.db import models
class Scoop(models.Model):
FLAVOR_CHOICES = [
('c', 'Chocolate'),
('v', 'Vanilla'),
]
flavor = models.CharField(choices=FLAVOR_CHOICES)
def flavor_verbose(self):
return dict(Scoop.FLAVOR_CHOCIES)[self.flavor]
My view passes a Scoop to the template (note: not Scoop.values()), and the template contains:
{{ scoop.flavor_verbose }}
Basing on Noah's reply, here's a version immune to fields without choices:
#annoyances/templatetags/data_verbose.py
from django import template
register = template.Library()
#register.filter
def data_verbose(boundField):
"""
Returns field's data or it's verbose version
for a field with choices defined.
Usage::
{% load data_verbose %}
{{form.some_field|data_verbose}}
"""
data = boundField.data
field = boundField.field
return hasattr(field, 'choices') and dict(field.choices).get(data,'') or data
I'm not sure wether it's ok to use a filter for such purpose. If anybody has a better solution, I'll be glad to see it :) Thank you Noah!
We can extend the filter solution by Noah to be more universal in dealing with data and field types:
<table>
{% for item in query %}
<tr>
{% for field in fields %}
<td>{{item|human_readable:field}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Here's the code:
#app_name/templatetags/custom_tags.py
def human_readable(value, arg):
if hasattr(value, 'get_' + str(arg) + '_display'):
return getattr(value, 'get_%s_display' % arg)()
elif hasattr(value, str(arg)):
if callable(getattr(value, str(arg))):
return getattr(value, arg)()
else:
return getattr(value, arg)
else:
try:
return value[arg]
except KeyError:
return settings.TEMPLATE_STRING_IF_INVALID
register.filter('human_readable', human_readable)
I don't think there's any built-in way to do that. A filter might do the trick, though:
#register.filter(name='display')
def display_value(bf):
"""Returns the display value of a BoundField"""
return dict(bf.field.choices).get(bf.data, '')
Then you can do:
{% for field in form %}
<tr>
<th>{{ field.label }}:</th>
<td>{{ field.data|display }}</td>
</tr>
{% endfor %}
You have Model.get_FOO_display() where FOO is the name of the field that has choices.
In your template do this :
{{ scoop.get_flavor_display }}
Add to your models.py one simple function:
def get_display(key, list):
d = dict(list)
if key in d:
return d[key]
return None
Now, you can get verbose value of choice fields like that:
class MealOrder(models.Model):
meal = models.CharField(max_length=8, choices=CHOICES)
def meal_verbose(self):
return get_display(self.meal, CHOICES)
Upd.: I'm not sure, is that solution “pythonic” and “django-way” enough or not, but it works. :)
The extended-extended version of Noah's and Ivan's solution. Also fixed Noah's solution for Django 3.1, as ModelChoiceIteratorValue is now unhashable.
#register.filter
def display_value(value: Any, arg: str = None) -> str:
"""Returns the display value of a BoundField or other form fields"""
if not arg: # attempt to auto-parse
# Returning regular field's value
if not hasattr(value.field, 'choices'): return value.value()
# Display select value for BoundField / Multiselect field
# This is used to get_..._display() for a read-only form-field
# which is not rendered as Input, but instead as text
return list(value.field.choices)[value.value()][1]
# usage: {{ field|display_value:<arg> }}
if hasattr(value, 'get_' + str(arg) + '_display'):
return getattr(value, 'get_%s_display' % arg)()
elif hasattr(value, str(arg)):
if callable(getattr(value, str(arg))):
return getattr(value, arg)()
return getattr(value, arg)
return value.get(arg) or ''
<select class="form-select">
{% for key, value in form.meal.field.choices %}
{% if form.meal.value == key %}
<option value="{{ form.key }}" selected>{{ value }}</option>
{% else %}
<option value="{{ key }}">{{ value }}</option>
{% endif %}
{% endfor %}
</select>
Hey what about that way?
in models.py
class MealOrder(models.Model):
CHOICES = (
('s', 'Glorious spam'),
('e', 'Fabulous eggs'),
)
meal = models.CharField(max_length=8, choices=CHOICES)
meal_value = models.CharField(max_length=1, blank=True, null=True,
editable=False)
def save(self, *args, **kwargs):
if self.meal == "s":
self.meal_value = "Glorious spam"
elif self.meal == "e":
self.meal_value = "Fabulous eggs"
super(MealOrder, self).save(*args, **kwargs)
in views.py
from .models import MealOrder
def meal_order(request):
meals = MealOrder.objects.all()
return render(request, "meals.html", {
"meals": meals,
})
in meals.html
{% for meal in meals %}
{{meal.meal_value }}
{%endfor%}