Reference¶
Functions¶
-
django_auto_url.urls.urls.
get_urls_from_module
(module)[source]¶ Scan a package and return the urlpatterns.
This function scans the module for all subclasses of
AutoUrlMixin
, generates aURLResolver
for each of them and returns a list that you can feed directly tourlpatterns
.Parameters: module (module) – The module to scan. Returns: list of URLResolver
Example
Assuming, you have your views defined in the module
my_app.views
, this is, how yoururls.py
would look like:>>> from my_app import views >>> from django_auto_url.urls import get_urls_from_module >>> >>> urlpatterns = get_urls_from_module(views)
-
django_auto_url.urls.urls.
reverse_classname
(class_name, return_url_name=False, args=None, kwargs=None)[source]¶ Get the URL for a View.
Converts a
View
into its URL or the django url name.Parameters: - class_name (str or subclass of
AutoUrlMixin
) – TheView
to reverse. You can either provide the class directly or a string with the full module path. - return_url_name (bool, optional) – If set to
True
, return the django url name and not the final URL. - args (list, optional) – The positional arguments for the view.
- kwargs (dict, optional) – The keyword arguments for the view.
Returns: str – The URL or the django url name.
- class_name (str or subclass of
-
django_auto_url.urls.urls.
reverse_classname_lazy
(class_name, return_url_name=False, args=None, kwargs=None)[source]¶ Lazy version of
reverse_classname()
.Instead of doing the reverse instantly, it is done, when the result is actually needed. This will help you when you need to specify a URL in a class attribute. The non-lazy version would instantly try to find the URL for the view, even if the view has not been declared yet (for instance, because it is declared later in the same file or in a different one).
Arguments and return values are exactly the same as
reverse_classname()
.
-
django_auto_url.urls.urls.
reverse_local_classname
(class_name, return_url_name=False, args=None, kwargs=None, lazy=True)[source]¶ Get the URL of a view declared in the same module.
Basically does the same as
reverse_classname()
but you do not need to specify the full module path to the view. The class name is enough.Parameters: - class_name (str or subclass of
AutoUrlMixin
) – TheView
to reverse. You can either provide the class directly or a string with the full module path. - return_url_name (bool, optional) – If set to
True
, return the django url name and not the final URL. - args (list, optional) – The positional arguments for the view.
- kwargs (dict, optional) – The keyword arguments for the view.
- lazy (bool, optional) – Determines whether the evaluation should be lazy.
Returns: str – The URL or the django url name.
- class_name (str or subclass of
Template Tags¶
-
url_from_class
¶ Return the absolute URL for a class, providing the full module path.
This is the equivalent to the url template tag of Django.
The only important difference is, that you can only use keyword arguments!
Example
{% load auto_url %} <a href="{% url_from_class "my_app.views.MyView" bool_arg="False" say_this="New String" my_age="32" %}">Click me!</a>
Parameters: - viewname (str) – The full module path to the view.
- **kwargs (any) – Keyword arguments for the view.
Returns: str – The absolute URL.
Mixins¶
-
class
django_auto_url.mixins.mixins.
AutoUrlMixin
(*args, **kwargs)[source]¶ Mixin for automatic URL url creation.
Include this Mixin in your
View
to have a urlpattern automatically generated when this surrounding package is scanned withget_urls_from_module()
.Variables: - url_path_name (str, optional) – If set, this is the string that appears in the actual URL. Otherwise the class name is used.
- url_django_name (str, optional) – If set, this is the name of the route in django. Otherwise the class name is used.
- is_index (bool) – Set to
True
if this is an index view. - url_kwargs (list of
kwarg
) – The list of Keyword Arguments for this view. - url_ignore_pk (bool) – If the
View
includes aslug_url_kwarg
orpk_url_kwarg
class attribute (like theDetailView
), a keyword argument for the Primary Key or the Slug is included automatically. Set this toFalse
if this is not what you want.
Keyword Arguments¶
Keyword arguments can be specified by using the url_kwargs
class variable of
the view. You can specify a default value, but in this case, the order matters:
You cannot define a Keyword argument without a default after one for which
you have specified one.
Example¶
class MyView(AutoUrlMixin, TemplateView):
url_kwargs = [
kwargs.String('my_string'),
kwargs.Int('my_int', 42)
]