Random Quotes

 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

  1. Filter for adding quote marks by olau 4 years, 2 months ago
  2. Random code tag by aaloy 4 years, 7 months ago
  3. Slideshow (Random Image Display) - Using Jquery by veeravendhan 2 years, 6 months ago
  4. "for" template tag with support for "else" if array is empty by jezdez 4 years ago
  5. Split a string to a list and add to select options by xuqingkuang 2 years, 9 months ago

Comments

(Forgotten your password?)