コード例 #1
0
def do_all_content(parser, token):
    """
    Returns all entries for a particular model.

    Example usage:

    {% load fetch_content %}
    {% get_all_content application_name.model_name as foo %}
    {% for bar in foo %}
        {{ bar.attribute }}
    {% endfor %}
    """
    bits = token.split_contents()
    if len(bits) != 4:
        raise template.TemplateSyntaxError("'get_all_content' takes exactly "
                                           "three arguments")
    model_args = bits[1].split('.')
    if len(model_args) != 2:
        raise template.TemplateSyntaxError("First argument to "
                                           "'get_all_content' must be an "
                                           "'application name'.'model name' "
                                           "string")
    try:
        # < Django 1.9
        model = get_model(*model_args)
    except:
        # Django 1.9
        model = AppConfig.get_model(*model_args)
    if model is None:
        raise template.TemplateSyntaxError("'get_all_content' tag got an "
                                           "invalid model: %s" % bits[1])
    return AllContentNode(model, bits[3])
コード例 #2
0
def do_latest_content(parser, token):
    """
    Returns a specific number of entries for a particular model.
    (If the model is sorted by date published they will be sorted that way
        hence the name get_latest_content.)

    Example usage:

    {% load fetch_content %}
    {% get_latest_content application_name.model_name 5 as foo %}
    {% for bar in foo %}
        {{ bar.attribute }}
    {% endfor %}
    """
    bits = token.split_contents()
    if len(bits) != 5:
        raise template.TemplateSyntaxError("'get_latest_content' takes "
                                           "exactly four arguments")
    model_args = bits[1].split('.')
    if len(model_args) != 2:
        raise template.TemplateSyntaxError("First argument to "
                                           "'get_latest_content' must be an "
                                           "'application name'.'model name' "
                                           "string")
    try:
        # < Django 1.9
        model = get_model(*model_args)
    except:
        # Django 1.9
        model = AppConfig.get_model(*model_args)
    if model is None:
        raise template.TemplateSyntaxError("'get_latest_content' tag got an "
                                           "invalid model: %s" % bits[1])
    return LatestContentNode(model, bits[2], bits[4])