- Author:
 - ferretsrule
 - Posted:
 - June 14, 2007
 - Language:
 - Python
 - Version:
 - .96
 - Score:
 - 1 (after 1 ratings)
 
Display a random quote. Just add some quotes to the app (you may want to add the administration interface options) and then load the template tag:
{% load random_quote %}
and then call the tag to display a random quote from the app
{% random_quote %}
feel free to improve it/whatever..
:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  | # model
class Quote(models.Model):
    quote = models.TextField(help_text="Enter the quote")
    by = models.CharField(maxlength=30, help_text="Enter the quote author")
    slug = models.SlugField(prepopulate_from=("by", "quote"), maxlength=25)
    def __str__(self):
        return (self.quote)
# template tag
from django import template
register = template.Library()
from website.quotes.models import Quote
@register.simple_tag
def random_quote():
    """
    Returns a random quote
    """
    quote = Quote.objects.order_by('?')[0]
 
    return str(quote)
 | 
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Please login first before commenting.