You could perhaps emulate something like that with a template tag, couldn't
you?
@register.simple_tag(takes_context=True)
def requires(context, *names):
for name in names:
if name not in context:
raise RuntimeError('{0} is not in the template
context'.format(name))
And in the template:
{% requires "name" "other" %}
Seems that it would be best if that kind of data lived with the template,
rather than outside in a Python file.
On 21 Sep 2017 14:37, "Zhiqiang Liu" <[email protected]> wrote:
> To continue the previous comment.
>
> template can raise error give warning if required contexts are not
> provided or the types are not correct. You can have something not
> isRequired in contextTypes too but types can be check if the context is
> actually passed to template.
>
>
> On Thursday, September 21, 2017 at 9:35:05 AM UTC-4, Zhiqiang Liu wrote:
>>
>> This is not 100% related to the ticket, but something to think about.
>>
>> In ReactJS, there a concept called propTypes, which will check all props
>> (they are similar to context in concept I think) listed with types in UI
>> component.
>>
>> So maybe we can have something similar in django template system that a
>> template can have an attribute called contextTypes and it can be a dict of
>> context the template may have. It doesn't need to have all possible context
>> values, only include those you want to make sure the context are passed and
>> value types are correct.
>>
>> A simple example can be
>>
>>
>> contextTypes = {
>> "name": contextTypes.string.isRequired,
>>
>> }
>>
>>
>>
>> On Thursday, September 21, 2017 at 7:05:28 AM UTC-4, Shreyas Pandya wrote:
>>>
>>> Hi All,
>>>
>>> What is your opinion on having an option to raise an error in template
>>> if variable is not found in context. This may be useful for automated
>>> tests as discussed in ticket.
>>>
>>> reference ticket #28618 <https://code.djangoproject.com/ticket/28618> ;
>>>
>>> Thanks
>>>
>>> regards
>>> Shreyas
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/ms
> gid/django-developers/0e82ce82-3a0d-44a6-9f68-f359a7354eb9%
> 40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/0e82ce82-3a0d-44a6-9f68-f359a7354eb9%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/CAFNZOJOcTUqeaBbeMy8OC%3D_Kf9_1D_Qz2x%3DyTBNSxEBKGgkDgA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.