Hello guys i’ve a simple question for django. I have a model like this:
Link_tr
Link_en
Link_fr
And at template i want to use like this: {{pk1.Link_{{LANGUAGE_CODE}}}} but django dont accept. I hope someone can solve my problem.
Hello guys i’ve a simple question for django. I have a model like this:
Link_tr
Link_en
Link_fr
And at template i want to use like this: {{pk1.Link_{{LANGUAGE_CODE}}}} but django dont accept. I hope someone can solve my problem.
Actually, this isn’t so much an I18n issue as it is a template / data attribute issue.
The quick answer is that there’s no built-in facility for retrieving a “variably named” field within the template system. You’ve got a couple of options:
If you need to do this a lot with many different models / fields, you may find #1 to be most useful. If you need to do it a lot but only on 1 field, #3 may be to your advantage. But if you’re only doing this once or twice with one or two fields, #2 may be the least work overall.
Mr whitesell, i understand your suggestion but because of the document’s language i can’t learn fully and solve it. If you or someone can make direct code i’ll be very very happy. Here’s my full code:
models.py
class Sektorler(models.Model):
Link_tr = models.SlugField(max_length=1000,verbose_name=“Link - Tr”,null=True,blank=True,unique=True)
Link_en = models.SlugField(max_length=1000,verbose_name=“Link - En”,null=True,blank=True,unique=True)
Isim_tr = models.CharField(max_length=200,verbose_name=“İsim - Tr”,null=True,blank=True)
Isim_en = models.CharField(max_length=200,verbose_name=“İsim - En”,null=True,blank=True)
def save(self, *args, **kwargs):
self.Link_tr = slugify(self.Isim_tr)
self.Link_en = slugify(self.Isim_en)
super(BuyukUrunCesitleri, self).save(*args, **kwargs)
At the template page for example index page:
{% for pk1 in productcategory1 %}
<div class="col" style="max-width: 200px">
<div class="card">
<img class="card-img-top" src="/static/Img1/ProductCategorys/{{pk1.Resim}}" alt="{{pk1.Isim_tr}}" />
{% union "pk1.Isim_" "current_language" as aaaa%}
<div class="card-body py-4 px-0 text-center">
<a class="stretched-link text-body" href="{% url 'products' productcategory='tasima-ekipmanlari' products=aaaa %}" >
<h6>{{aaaa}}<small>(11)</small></h6>
</a>
</div>
</div>
</div>
{% endfor %}
First I made a simple tag who union strings but it didn’t work for example return of {{aaaa}} = pk1.Isim1_tr(or en) but i want to value of pk1.Isim1_tr(or en). If you understand me, can you write direct code of ‘getattr’ or be more spesific(For 2 full day i’m working on it and now i’m starting to become crazy).
You can start from this snippet as a base. You’ll have to modify it to work with two arguments - the base field name and the language code. (You’ll do the concatenation in the function, not in the template.)