Welcome to django_auto_url’s documentation!

django_auto_url is a Django app that liberates you from maintaining the urls.py files.

The principle is that a website (or a webapp) basically consists of a bunch of View and the user navigates from one View to the next or gets directed to View after some interaction.

So why the hassle with manually creating URL patterns and names?

django_auto_url provides functions and mixins to automate this process.

Quickstart

Install

Configure

Next, include django_auto_url in the INSTALLED_APPS section of the Django settings.py:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_auto_url'
    ]

Prepare the views

Now include the AutoUrlMixin in the mixins of your view class:

Attention

Always include mixins before the view class!

from django.views.generic import TemplateView
from django_auto_url.mixins import AutoUrlMixin

class MyView(AutoUrlMixin, TemplateView):
    template_name = 'my_template.html'

Prepare urls.py

Now we need to generate the urlpatterns for our view in the urls.py file.

The urls.py file would look like this:

from my_app import views
from django_auto_url.urls import get_urls_from_module

urlpatterns = get_urls_from_module(views)

Get the URL for the view

django_auto_url provides several methods to get the URL of a view.

Use the template tag

Getting the absolute URL for a view in Django is done using the url template tag. django_auto_url provides a similar template tag, called url_from_class that performs the same task but takes the full module path to a view as the argument.

Let’s consider that the view class we created above lives in the views package of your app called my_app.

Here is how to create a link to it from a template:

{% load auto_url %}

<a href="{% url_from_class "my_app.views.MyView" %}">Click me!</a>

Use the Python functions

In order to get the URL for a view, use one of the following functions:

All these function are quite similar in how they work: You provide the view class or the full module path as a string and you get the URL returned.

from my_app import views
from django_auto_url.urls import reverse_classname

# resolve now. views.MyView must have already been declared.
url_for_view = reverse_classname(views.MyView)
url_for_view = reverse_classname('my_app.views.MyView')

# resolve later. views.MyView can be declared later.
# This is very useful if you need to provide a URL as a class variable.
url_for_view = reverse_classname_lazy(views.MyView)
url_for_view = reverse_classname_lazy('my_app.views.MyView')

For further details, please refer to the appropriate section in the reference.

Use Arguments for the View

As usual, views can accept arguments by their URL patterns. You can do this for your AutoUrlMixin views by specifying url_kwargs like this:

from django.views.generic import TemplateView
from django_auto_url.mixins import AutoUrlMixin
from django_auto_url import kwargs

class MyView(AutoUrlMixin, TemplateView):
    template_name = 'my_template.html'
    url_kwargs = [
        kwargs.String('my_string'),
        kwargs.Int('my_int', 42)
    ]

In this case, we have specified that the view takes two arguments, one string and one int. And there is a default value for the integer value.

The values of these arguments are provided as kwargs to the respective methods of the view as well as to the context.

From your template, here is how you would link to it:

{% load auto_url %}

<a href="{% url_from_class "my_app.views.MyView" my_string="Hello World" %}">Click me!</a>
<a href="{% url_from_class "my_app.views.MyView" my_string="Hello" my_int=32 %}">Click me, too!</a>

The respective python function provide a args and kwargs parameter you can use:

from my_app import views
from django_auto_url.urls import reverse_classname

# resolve now. views.MyView must have already been declared.
url_for_view = reverse_classname(views.MyView, kwargs = {
                                                    'my_string': 'Hello World'
                                                })

Indices and tables