Getting Started

This section will teach you how to use django-quotes to create, edit, and delete quotes easily.

Creating Quotes Using the Admin Panel

If you want to be able to create, edit, moderate, and remove Quote s easily, you can simply visit the Django admin panel.

Using the admin panel requires that you’ve already completed the previous section, Setting up URLs.

To get started, launch your Django site and log into your admin site.

Click on the link labeled Quotes, and you’ll be brought to the Quote management page. Here you can create new quotes, delete quotes, approve quotes, and edit Quotes.

Creating Quotes Using the API

If you would like to pragmatically create Quote objects, you can do so using the simple API.

Before you get started, you’ll need to import the Quote model in your Django project. All examples in this section are shown in the Django python shell.

Note

To access the Django python shell, go into the root directory of your Django project, and type python manage.py shell.

Import the Quote model:

>>> from quotes.models import Quote

Now, let’s create a Quote using all the available attributes:

>>> q = Quote.objects.create(quote='Programmer, hack thyself.',
...             author='John Walker', source='http://google.com/', approved=True)

The available attributes that can be specified are:

quote The quote to store.

author The person who is known for this quote.

source A URL which points to a web page that can be used to verify the validity of this quote.

approved Whether or not this quote has been approved. Only quotes that have been approved will be shown when using the built in templatetags.

If no author is specified during object creation than the author attribute will default to the value specified by DEFAULT_AUTHOR (which defaults to ‘Unknown’):

>>> q = Quote.objects.create(quote='E=MC^2')
>>> q.author
'Unknown'
>>> q.DEFAULT_AUTHOR
'Unknown'

By default, any new Quote you create will NOT be approved:

>>> q = Quote.objects.create(quote='OH HAI')
>>> q.approved
False

If you don’t really care about the approval process for Quote s, you can easily bulk-approve them:

>>> Quote.objects.all().update(approved=True)

If you need more help creating Quote, consult the api documentation.

Displaying a Random Quote

To display a random quote, django-quotes includes a template tag, show_random_quote() , which can be easily integrated into any Django template.

In your template, simply load the template tags:

{% load quotes_tags %}

Then wherever you want to display a random quote, add the following:

{% show_random_quote %}

And that’s it! django-quotes will pull a random quote from the database, then format it into HTML using the following format:

<div class="quote">
        <blockquote>Here there be monsters...</blockquote>
        <span class="author">-- Unknown</span>
</div>

Want to change the default HTML formatting? No problem. Just override the quote.html template:

  1. In your templates directory (usually SITE_ROOT/templates), create a sub-folder names ‘quotes’.

  2. Inside the ‘quotes’ directory, create a new template, ‘quote.html’. You can use the default template provided by django-quotes as a starting point:

    {% if quote %}
            <div class="quote">
                    <blockquote>{{ quote.quote }}</blockquote>
                    <span class="author">-- {{ quote.author }}</span>
            </div>
    {% endif %}

Note

If you decide to change the quote.html template, be aware that it will effect the other template tags as well. This is because both of the template tags that django-quotes provides use the same quote.html template to render quotes.

Displaying a Known Quote

In some situations, you may want to only display a known Quote. django-quotes provides a template tag for doing just that, and all it requires is the id of the Quote that you want to display.

As stated in the previous section, in order to use the template tags, you must first load them into your Django template:

{% load quotes_tags %}

Then just add the following tag wherever you wish to display the desired Quote

{% show_quote 1 %}

Where 1 is the id of the Quote you wish to display.

Table Of Contents

Previous topic

Installation

This Page