10👍
https://github.com/mlouro/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.
It currently supports the following features:
- Reverse method for generating Django urls…
19👍
Try creating javascript helper functions (in django template) for generating url string. In simple form they could look like this:
function generete_some_url(id){
return "{% url some_url itemid=112233 %}".replace("112233", id);
}
Maybe this has some other implications but I think it should work.
- [Django]-How to find user id from session_data from django_session table?
- [Django]-Django: Where to put helper functions?
- [Django]-How to paginate Django with other get variables?
14👍
What’s wrong with putting JavaScript in your templates?
You often want to call an initialisation function in your HTML template anyway, so why not pass it an object containing URLs you’ll be using?
<script>
MYGLOBAL.mymodule.init({
fancy_ajax_url: '{% url fancy %}',
fancier_ajax_url: '{% url fancier %}'
});
</script>
If you find yourself passing a lot of variable this way, or wanting to use logic in your JavaScript that you do in your HTML templates, then why not render your script through Django’s templating engine? Remember, Django templates are not just for HTML documents – often it helps to use templates for plain text, XML, JSON, and yes even JavaScript. Worried about performance? Then cache the result.
- [Django]-Trying to trace a circular import error in Django
- [Django]-Django URL Redirect
- [Django]-Are there any plans to officially support Django with IIS?
12👍
I created a mechanism that builds a list of url patterns in your Django project and outputs that in a Javascript file. It is a fork of django-js-utils.
The repo link is here:
https://github.com/Dimitri-Gnidash/django-js-utils
- [Django]-Nginx uwsgi (104: Connection reset by peer) while reading response header from upstream
- [Django]-Django: raise BadRequest as exception?
- [Django]-Django reverse lookup of foreign keys
7👍
We created a small app called django-js-reverse for this purpose.
For example you can retrieve a named url
urls.py:
url(r’^/betterliving/(?P[-\w]+)/(?P\d+)/$’, ‘get_house’, name=’betterliving_get_house’),
in javascript like:
Urls.betterliving_get_house(‘house’, 12)
result:
/betterliving/house/12/
- [Django]-Django TextField and CharField is stripping spaces and blank lines
- [Django]-What is the most efficient way to store a list in the Django models?
- [Django]-React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)
4👍
What I usually do is put the URL in either an <input type="hidden" />
element, or in the rel=""
attribute.
Then, when writing the JS (using jQuery below) I do:
$('div#show_more').click(function () {
var url = $(this).attr('rel');
// or
var url = $('#more_url').val();
$.get(url, function () { /* ... */ });
});
Nonstandard attributes are well supported by all major browsers and hidden elements don’t have to be in forms.
- [Django]-Django error: needs to have a value for field "…" before this many-to-many relationship can be used
- [Django]-How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication
- [Django]-Why is __init__ module in django project loaded twice
2👍
First, you should name your url:
url(r'^blog/(?P<item_id>\d+)/$', 'blog.ajax.remove_item', name='blog-item'),
Then you could pass urls as variables to your module:
<script src="{{ STATIC_URL }}js/my-module.js"></script>
<script>
$(function(){
MyModule.init('{% url blog-item item.id %}');
});
</script>
// js/my-module.js
var MyModule = {
init: function(url) {
console.log(url);
}
};
You could use tokens in your url:
<script src="{{ STATIC_URL }}js/my-module.js"></script>
<script>
$(function(){
MyModule.init("{% url blog-item item_id='0000' %}");
});
</script>
// js/my-module.js
var MyModule = {
init: function(url) {
var id = 1;
this._url = url;
console.log(this.url(id));
},
url: function(id) {
return this._url.replace('0000', id);
}
};
Notice that your token should match the regex type to resolve successfully (I can’t use {item_id}
as token because it’s defined with \d+
).
I was a little bit unsatisfied with this solution and I ended by writing my own application to handle javascript with django: django.js. With this application, I can do:
{% load js %}
{% django_js %}
{% js "js/my-module.js" %}
// js/my-module.js
var MyModule = {
init: function() {
var id = 1;
console.log(Django.url('blog-item', id));
}
};
$(function(){
MyModule.init();
});
- [Django]-Adding attributes into Django Model's Meta class
- [Django]-How to change site title, site header and index title in Django Admin?
- [Django]-GeoDjango on Windows: "Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found"
2👍
You can remove the parameters from the URL, and pass the dynamic parts as query parameters:
$('#add-choice-button').on('click', function () {
var thing_id = $(this).closest('.thing').attr('data-item-id');
$.get('{% url 'addThing' %}?t='+thing_id, function (data) {
...
});
});
- [Django]-Nested blocks in Django templates
- [Django]-Django equivalent of COUNT with GROUP BY
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
1👍
I have found this cool django app called Django JS reverse
https://github.com/ierror/django-js-reverse
If you have a url like
url(r'^/betterliving/(?P<category_slug>[-\w]+)/(?P<entry_pk>\d+)/$', 'get_house', name='betterliving_get_house'),
Then you do
Urls.betterliving_get_house('house', 12)
The result is
/betterliving/house/12/
- [Django]-Django model class methods for predefined values
- [Django]-Installing lxml with pip in virtualenv Ubuntu 12.10 error: command 'gcc' failed with exit status 4
- [Django]-Django model Form. Include fields from related models