django-templates

Change row colour in Django Admin List

五迷三道 提交于 2019-12-29 06:00:17
问题 I want to highlight rows (set the backgorund colour) in the Django Admin page (change_list.html) based on a field in the model called status. What is the best way to do this? I have 3 statuses. Open, Active, Closed. I'd like rows with open to be green, active to be orange and closed to be red. I've found documentation about changing the templates so but not sure how to check the status to colour the row. 回答1: Check out django-liststyle, exactly what you're after. 回答2: I was wrong, there's

Change row colour in Django Admin List

情到浓时终转凉″ 提交于 2019-12-29 05:59:20
问题 I want to highlight rows (set the backgorund colour) in the Django Admin page (change_list.html) based on a field in the model called status. What is the best way to do this? I have 3 statuses. Open, Active, Closed. I'd like rows with open to be green, active to be orange and closed to be red. I've found documentation about changing the templates so but not sure how to check the status to colour the row. 回答1: Check out django-liststyle, exactly what you're after. 回答2: I was wrong, there's

Django Templating: how to access properties of the first item in a list

房东的猫 提交于 2019-12-29 02:46:08
问题 Pretty simple. I have a Python list that I am passing to a Django template. I can specifically access the first item in this list using {{ thelist|first }} However, I also want to access a property of that item... ideally you'd think it would look like this: {{ thelist|first.propertyName }} But alas, it does not. Is there any template solution to this, or am I just going to find myself passing an extra template variable... 回答1: You can access any item in a list via its index number. In a

Django Templating: how to access properties of the first item in a list

这一生的挚爱 提交于 2019-12-29 02:46:06
问题 Pretty simple. I have a Python list that I am passing to a Django template. I can specifically access the first item in this list using {{ thelist|first }} However, I also want to access a property of that item... ideally you'd think it would look like this: {{ thelist|first.propertyName }} But alas, it does not. Is there any template solution to this, or am I just going to find myself passing an extra template variable... 回答1: You can access any item in a list via its index number. In a

Django - initial checked CheckboxInput

ε祈祈猫儿з 提交于 2019-12-29 02:14:05
问题 I want my CheckboxInput was checked by default : My models : class Sub(models.Model): user = models.OneToOneField(User) batata = models.BooleanField(default=False) class presentationForm(forms.ModelForm): class Meta : model = Sub widgets = { 'batata': forms.CheckboxInput(attrs={'id':'batata'}), } def __init__(self, *args, **kwargs): self.valbata = kwargs.pop("arg_bata") super(presentationForm, self).__init__(*args, **kwargs) self.fields['batata'].initial = True My view : def theview(request):

Model name of objects in django templates

爷,独闯天下 提交于 2019-12-28 12:46:55
问题 Is there any way to get the model name of any objects in django templates. Manually, we can try it by defining methods in models or using template tags... But is there any built-in way? 回答1: object.__class__.__name__ or object._meta.object_name should give you the name of the model class. However, this cannot be used in templates because the attribute names start with an underscore. There isn't a built in way to get at that value from the templates, so you'll have to define a model method

Django grouping queryset by first letter?

て烟熏妆下的殇ゞ 提交于 2019-12-28 12:06:08
问题 I have a QuerySet like: items = Item.objects.all() Item has a 'name' field. In the template I want to show: A Axes Alcohol B Bazookas C Coins Cartridges S Swords Sparrows So the items are ordered and group by the first letter. Missing letters are omitted. Does anyone have any ideas? 回答1: There's a template tag for this, if all you care about is its presentation on the page. First, define an organizational principle in the class. In your case, it's the first letter: class Item(models.Model): .

Django templates: Get current URL in another language

冷暖自知 提交于 2019-12-28 09:14:17
问题 I'm using i18n_patterns to create language prefixes in a Django app. My URLs look like this: /de/contact/ /fr/contact/ /it/contact/ In my base template, I'm looping over all available languages to show the language switch links. {% get_available_languages as languages %} <nav id="language_chooser"> <ul> {% for lang_code, lang_name in languages %} {% language lang_code %} <li><a href="{% url 'home' %}" alt="{{ lang_name }}" title="{{ lang_name }}">{{ lang_code }}</a></li {% endlanguage %} {%

django template system, calling a function inside a model

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-27 13:38:09
问题 I want to call a function from my model at a template such as: class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: for i in self.get_listof_outage(): item = i.error_code.all() for x in item: if epk == x.id: mystring= mystring +" "+str(i.delta()) return mystring And when I want to call this from the template: assume while rendering, I pass

django template system, calling a function inside a model

依然范特西╮ 提交于 2019-12-27 13:37:28
问题 I want to call a function from my model at a template such as: class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: for i in self.get_listof_outage(): item = i.error_code.all() for x in item: if epk == x.id: mystring= mystring +" "+str(i.delta()) return mystring And when I want to call this from the template: assume while rendering, I pass