跳转到主要内容

Django URL转换器

项目描述

django-urlconverter:Django自动视图参数转换

django-urlconverter软件包提供了一种简单的方法来将视图参数从简单类型转换为复杂类型,如模型,或者使用强大的语法进行一些处理。该软件包最有意思的应用是将对象ID(通过ORM数据库)的视图参数转换为这些对象的实例(模型)。

https://api.travis-ci.org/paylogic/django-urlconverter.png https://pypip.in/v/django-urlconverter/badge.png https://coveralls.io/repos/paylogic/django-urlconverter/badge.png?branch=master

安装

pip install django-urlconverter

用法

models.py

from django.db import models


class FooBar(models.Model):
    """Foo Bar model."""
    title = models.CharField(max_length=100)

converters.py

from .models import FooBar


class Foo(object):
    """Foo converter. Convert an object id to FooBar instance."""
    def __init__(self, **kwargs):
        self.kwargs = kwargs

    def __call__(self, value, request, *args, **kwargs):
        return FooBar.objects.get(pk=value)

settings.py

URL_CONVERTERS = {
    'Foo': 'converters.Foo',  # Converts to 'FooBar instance'
}

urls.py

from django.conf.urls.defaults import patterns
from django_urlconverter import converted_arguments_url

urlpatterns = patterns('views',
    converted_arguments_url(r'^foos/<Foo:foo2>/', 'bar'),
)

views.py

def bar(request, foo2):
    """View function which gets a converted foo2.
    the foo2 will be a converted parameter, which is the result of Foo converter
    """
    return foo2.title

联系

如果您有任何问题、错误报告、建议等,请在GitHub项目页面上创建一个问题。

许可证

本软件根据MIT许可证授权

https://github.com/paylogic/django-urlconverter/blob/master/LICENSE.txt

© 2013 Paylogic International.

变更日志

1.0.0

  • 首次公开发布

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

django-urlconverter-1.0.0.tar.gz (5.5 kB 查看散列)

上传时间:

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面