Reputation: 311
I apologize for the stupid question. This is my very first experience with OOP.
fseries(self)
? I understand that the problem is with parenthesis. I have a number of similar calculated values, which need to be rendered. Shall I calculate / define all
these values in class methods or somehow else?get_object_or_404(afc_series, valuefg = self.series)
or shall I use
another construction?#views.py
...
myfilter = Ksf_filter(name="mysuperfilter_XXX", series="ABCDE")
#just testing correctness
print(myfilter.name)
print(myfilter.fseries())
print(myfilter.fseries().valueecp)
context = {'myfilter':myfilter,}
return render(request, 'af/size/01_kss_size2.html', context)
#ksf_filter.py
from af.models import afc_series
from django.shortcuts import get_object_or_404
class Ksf_filter:
def __init__(self, name, series):
self.name = name
self.series = series
def fseries(self):
return get_object_or_404(afc_series, valuefg = self.series)
#template.html
{{myfilter.name}} ///// {{myfilter.fseries()}} //// {{myfilter.fseries().valueecp}})
This is the error message, I get.
#Error_msg
TemplateSyntaxError at /ru/af/n01_size_kss/
Could not parse the remainder: '()' from 'myfilter.fseries()'
Request Method: POST
Request URL: http://127.0.0.1:5000/ru/af/n01_size_kss/
Django Version: 3.0.5
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '()' from 'myfilter.fseries()'
Thank you
Upvotes: 1
Views: 229
Reputation: 477437
Django template variables automatically call a callable without extra parameters. So you can render this with:
{{ myfilter.fseries }} {{ myfilter.fseries.valueecp }}
The Django template language is deliberately restricted to do more advanced function calls to prevent people from writing business logic in the templates.
In case you need more advanced processing, you normally do this in the view. You can also use more extended template language like Jinja.
Question 2: Is it correct that I am getting the object via
get_object_or_404(afc_series, valuefg = self.series)
or shall I use another construction?
If valuefg
is unique, then this can be used as an "identifier". Using get_object_or_404
is then a good idea, since it returns a 404 in case it can not find the element. You however should make sure that there is (at most) one value that can match.
Upvotes: 1