GithubHelp home page GithubHelp logo

django-model-urls's Introduction

Django Model URLs

Build Status

This app makes urls usage DRYer. It maps URL keywords to a model object instance by passing the object instance as argument to the URL.

In short, having an article model instance, you can write this:

<a href="{{ url('article:detail', article) }}">View this article</a>

While you have been writing this so far:

<a href="{{ url('article:detail', year=article.year, month=article.month, slug=article.slug) }}">View this article</a>

This is for your templates as well as for your reverse() and preserves other urls to work.

Right now it is build for Jinja2 using the easy-to-use Jingo adapter. If you are using plain Django template, refer to version 0.3.1. A new version for plain Django should come out later on.

Installation

Download via pip pip badge

pip install django-model-urls

or get the bleeding edge version:

pip install git+https://github.com/vinyll/django-model-urls.git

Add model_urls to your settings file:

INSTALLED_APPS = (
    'jingo',
    'model_urls',
)

And you're done!

How to use it

In the examples below we will consider this model:

class Article(models.Model):
    slug = models.SlugField()
    title = models.CharField(max_length=50)
    date = models.DateField()

    @property
    def year(self):
        return self.date.year

    @property
    def month(self):
        return self.date.month

and this urls:

urlpatterns = patterns('',
    url(r'^article/(?P<year>\d{4})/(?P<month>\d{2})/(?P<slug>[\w-]+)/$',
        ArticleView.as_view(), name="article_details"),
)

and article being an instance of the Article class.

In a template

url('article_details', article)

In a view

from model_urls.urlresolvers import reverse

reverse('article_details', article)

Both will generate a url in this format:

/2014/11/how-to-optimize-django-urls/

Extra tools

Basically you should be able to use url() and reverse() in all cases. However, these tools are also available:

Template helpers

  • url(viewname, *args, **kwargs) will try to detect an instance in arguments and choose wheter to use simple_url() or model_url() otherwise.
  • simple_url(viewname, *args, **kwargs) is an alias to Jingo's url() helper to force using it.
  • model_url(viewname, instance, *args, **kwargs) will generate the url from the instance in first argument.

Url reversing

These functions are available in model_urls.urlresolvers.

  • model_reverse(viewname, instance, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None) will generate the url based on the instance properties.
  • reverse(viewname, *args, **kwargs) will fallback to model_reverse() if an instance is found in arguments or to Django's reverse() otherwise.

Configuration

An optional configuration is for your settings is:

MODEL_URLS_HELPER_OVERRIDE = False

This option will not allow the Jingo's url() helper to be overriden by the one from model_urls.

In this case you should use model_url() to use an instance.

Further examples

Use cases

A common use case is switching from pk based url to slug. Using django-model-urls means updating the urls.py file to consider slug without altering views or template files.

Read tests

Refer to tests.py to see more usages.

django-model-urls's People

Contributors

vinyll avatar wo0dyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

django-model-urls's Issues

[QUESTION] is this app compatible with django-jinja (since jingo is deprecated)?

As per the question is this app compatible with django-jinja app (link) ?
If yes I'm not able to use the url tag:

in my content.html:

{% for item in list_of_adv %}
href="{{url('traits' traits=item)}}">{{item.name}}
{% endfor %}

in my views.py

def traits(request):
model_reverse('traits', traits)

in my urls.py

url(r'^traits/(?P<name>.+)|(?P<unique_id>.+)/$', views.traits, name='traits'),

and I have a traits.jinja partial. But i get a template error in the view where the url tag is... Any thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.