python - Django Templatetag, List index out of range -
I have found a simple template that gives a random fact. When there is no fact in the database, I am getting an exception value Out of range I skipped the query set except for leaving * and also checking that There is a fact in the template. You can see the code given below: fact.py fact.html Try it out:
import the facts.models from the template template django import template = register template. Library () def random_fact (): try: fact = fact (. ')' [0] except fact. Not Axis: Fact = Any Return {Facts}: Facts} register.inview_tag ('facts / fact.html') (random_fact)
& lt ;! - Random Fact Start - & gt; & Lt; Div class = "panel panel-default sidebar" & gt; & Lt; div class = "panel-title" & gt; & Lt; H3 class = "panel-title" & gt; Do you know? & Lt; / H3 & gt; & Lt; / Div & gt; & Lt; Div class = "panel-body" & gt; {% If fact%} and lt; P class = "text-left" & gt; {{Fact.fact}} & lt; / P & gt; & Lt; P class = "text-right" & gt; & Lt; I class = "fa fa-link" & gt; & Lt; / I & gt; & Lt; I & gt; & Lt; A href = "{{fact.website}}" target = "_ blank" & gt; {{Fact.author}} & lt; / A & gt; & Lt; / I & gt; & Lt; / P & gt; {% Else%} & lt; P class = "text-left" & gt; There are currently no study tips! & Lt; / P & gt; {% Endif%} & lt; / Div & gt; & Lt; / Div & gt; & Lt ;! - Random Factors End - & gt;
def random_fact () (Fact): fact}
Comments
Post a Comment