I am working on an app that requires changing a flask template to that of Django.How to change the url_for('function') in the html page
I am working on an app that requires changing a flask template to that of Django.How to change the url_for('function') in the html page
In Django you can use:
{% url 'some-url-name' %}
You can also add params as follows:
{% url 'some-url-name' param1=val1 param2=val2 %}
With some-url-name
is the name that you give to the url
associated with your view
like the following:
url(r'^some-url/', 'myapp.views.some_view', name='some-url-name')
If you want to use url namespaces, an example looks like this (we suppose some-url-name
is the url name in myapp/urls.py
):
url(r'^some-url/', include('myapp.urls', namespace='myapp'))
In your template:
{% url 'myapp:some-url-name' param1=val1 param2=val2 %}
Some differences with static files:
In Flask:
url_for('static', filename='path/to/file')
In Django:
{% static "path/to/file" %}