Giter Club home page Giter Club logo

django-js-utils's Introduction

dutils - Django JS Utils

dutils is a small utility library that aims to provide JavaScript/Django developers with a few utilities that will help the development of RIA on top of a Django Backend.

Reversing Django Urls from Javascript

Why is this useful

One of the pillars of Django is DRY principle and hardcoding your urls in Javascript is violating that principle.

Moreover, building parametrized urls on the fly is error-prone and ugly.

What is included

A snippet of Javascript implementation of Django reverse function that can be found in dutils.js

A management command js_urls to generate a list of all of your Django urls

Installation

  1. Install package using pip:

    pip install -e hg+https://github.com/Dimitri-Gnidash/django-js-utils.git#egg=django-js-utils
    
  2. Append app to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS += ('django_js_utils',)
    
  1. Set the name of the file that will be generated by the management command in your project settings.py:

    # Where to put the Javascript URL Routing file
    URLS_JS_GENERATED_FILE='/static/js/django-urls.js'
    

3. Add dutils.js and urls routing Javascript file to every web page where you plan to use the reverse function (likely just include in your base.html template), e.g.:

<script src="{{ STATIC_URL }}js/dutils.js"></script>
<script src="{{ STATIC_URL }}js/django-urls.js"></script>

Usage

1. To generate a list of all available urls in the special format:

>>> python manage.js js_urls

To keep the list of urls up-to-date, it is recommended to include this command as part of the build process.

2. On the web page, reverse url as such:

>>> $.post(dutils.urls.resolve('time_edit', { project_id: 1, time_id: 2 }), ...

For more usage, see example.html

TO-DO

  1. Handle the unnamed Django urls that result in <> in urls.js file, but are not handled in Javascript resolver.
  2. Write unit tests
  3. Promote the code

django-js-utils's People

Contributors

mlouro avatar dimitri-gnidash avatar wolfe avatar cypreess avatar strongholder avatar drx avatar

Watchers

 avatar

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.