367
338
All parameters should be Unicode objects, except ``categories``, which
368
339
should be a sequence of Unicode objects.
370
.. method:: add_item(title, link, description, [author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, unique_id=None, enclosure=None, categories=(), item_copyright=None, ttl=None, **kwargs])
341
.. method:: add_item(title, link, description, [author_email=None, author_name=None, author_link=None, pubdate=None, comments=None, unique_id=None, enclosure=None, categories=(), item_copyright=None, ttl=None, updateddate=None, **kwargs])
372
343
Adds an item to the feed. All args are expected to be Python ``unicode``
373
objects except ``pubdate``, which is a ``datetime.datetime`` object, and
374
``enclosure``, which is an instance of the ``Enclosure`` class.
344
objects except ``pubdate`` and ``updateddate``, which are ``datetime.datetime``
345
objects, and ``enclosure``, which is an instance of the ``Enclosure`` class.
347
.. versionadded:: 1.7
349
The optional ``updateddate`` argument was added.
376
351
.. method:: num_items()
508
486
.. function:: allow_lazy(func, *resultclasses)
510
Django offers many utility functions (particularly in ``django.utils``) that
511
take a string as their first argument and do something to that string. These
512
functions are used by template filters as well as directly in other code.
488
Django offers many utility functions (particularly in ``django.utils``)
489
that take a string as their first argument and do something to that string.
490
These functions are used by template filters as well as directly in other
514
493
If you write your own similar functions and deal with translations, you'll
515
face the problem of what to do when the first argument is a lazy translation
516
object. You don't want to convert it to a string immediately, because you might
517
be using this function outside of a view (and hence the current thread's locale
518
setting will not be correct).
494
face the problem of what to do when the first argument is a lazy
495
translation object. You don't want to convert it to a string immediately,
496
because you might be using this function outside of a view (and hence the
497
current thread's locale setting will not be correct).
520
499
For cases like this, use the ``django.utils.functional.allow_lazy()``
521
500
decorator. It modifies the function so that *if* it's called with a lazy
522
translation as the first argument, the function evaluation is delayed until it
523
needs to be converted to a string.
501
translation as one of its arguments, the function evaluation is delayed
502
until it needs to be converted to a string.
532
511
# Replace unicode by str on Python 3
533
512
fancy_utility_function = allow_lazy(fancy_utility_function, unicode)
535
The ``allow_lazy()`` decorator takes, in addition to the function to decorate,
536
a number of extra arguments (``*args``) specifying the type(s) that the
537
original function can return. Usually, it's enough to include ``unicode``
538
(or ``str`` on Python 3) here and ensure that your function returns only
514
The ``allow_lazy()`` decorator takes, in addition to the function to
515
decorate, a number of extra arguments (``*args``) specifying the type(s)
516
that the original function can return. Usually, it's enough to include
517
``unicode`` (or ``str`` on Python 3) here and ensure that your function
518
returns only Unicode strings.
541
520
Using this decorator means you can write your function and assume that the
542
input is a proper string, then add support for lazy translation objects at the
521
input is a proper string, then add support for lazy translation objects at
545
524
``django.utils.html``
546
525
=====================
746
727
Functions for working with Python modules.
729
.. function:: import_string(dotted_path)
731
.. versionadded:: 1.7
733
Imports a dotted module path and returns the attribute/class designated by
734
the last name in the path. Raises ``ImportError`` if the import failed. For
737
from django.utils.module_loading import import_string
738
ValidationError = import_string('django.core.exceptions.ValidationError')
742
from django.core.exceptions import ValidationError
748
744
.. function:: import_by_path(dotted_path, error_prefix='')
750
746
.. versionadded:: 1.6
748
Use :meth:`~django.utils.module_loading.import_string` instead.
752
750
Imports a dotted module path and returns the attribute/class designated by
753
the last name in the path. Raises
754
:exc:`~django.core.exceptions.ImproperlyConfigured` if something goes
757
from django.utils.module_loading import import_by_path
758
ImproperlyConfigured = import_by_path('django.core.exceptions.ImproperlyConfigured')
762
from django.core.exceptions import ImproperlyConfigured
751
the last name in the path. Raises :exc:`~django.core.exceptions.ImproperlyConfigured`
752
if something goes wrong.
764
754
``django.utils.safestring``
765
755
===========================
832
818
.. function:: slugify
834
820
Converts to lowercase, removes non-word characters (alphanumerics and
835
underscores) and converts spaces to hyphens. Also strips leading and trailing
821
underscores) and converts spaces to hyphens. Also strips leading and
842
If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
844
``django.utils.translation``
845
============================
847
.. module:: django.utils.translation
848
:synopsis: Internationalization support.
850
For a complete discussion on the usage of the following see the
851
:doc:`translation documentation </topics/i18n/translation>`.
853
.. function:: gettext(message)
855
Translates ``message`` and returns it in a UTF-8 bytestring
857
.. function:: ugettext(message)
859
Translates ``message`` and returns it in a unicode string
861
.. function:: pgettext(context, message)
863
Translates ``message`` given the ``context`` and returns
864
it in a unicode string.
866
For more information, see :ref:`contextual-markers`.
868
.. function:: gettext_lazy(message)
869
.. function:: ugettext_lazy(message)
870
.. function:: pgettext_lazy(context, message)
872
Same as the non-lazy versions above, but using lazy execution.
874
See :ref:`lazy translations documentation <lazy-translations>`.
876
.. function:: gettext_noop(message)
877
.. function:: ugettext_noop(message)
879
Marks strings for translation but doesn't translate them now. This can be
880
used to store strings in global variables that should stay in the base
881
language (because they might be used externally) and will be translated
884
.. function:: ngettext(singular, plural, number)
886
Translates ``singular`` and ``plural`` and returns the appropriate string
887
based on ``number`` in a UTF-8 bytestring.
889
.. function:: ungettext(singular, plural, number)
891
Translates ``singular`` and ``plural`` and returns the appropriate string
892
based on ``number`` in a unicode string.
894
.. function:: npgettext(context, singular, plural, number)
896
Translates ``singular`` and ``plural`` and returns the appropriate string
897
based on ``number`` and the ``context`` in a unicode string.
899
.. function:: ngettext_lazy(singular, plural, number)
900
.. function:: ungettext_lazy(singular, plural, number)
901
.. function:: npgettext_lazy(context, singular, plural, number)
903
Same as the non-lazy versions above, but using lazy execution.
905
See :ref:`lazy translations documentation <lazy-translations>`.
907
.. function:: string_concat(*strings)
909
Lazy variant of string concatenation, needed for translations that are
910
constructed from multiple parts.
912
.. function:: activate(language)
914
Fetches the translation object for a given language and installs it as
915
the current translation object for the current thread.
917
.. function:: deactivate()
919
De-installs the currently active translation object so that further _ calls
920
will resolve against the default translation object, again.
922
.. function:: deactivate_all()
924
Makes the active translation object a NullTranslations() instance. This is
925
useful when we want delayed translations to appear as the original string
928
.. function:: override(language, deactivate=False)
930
A Python context manager that uses
931
:func:`django.utils.translation.activate` to fetch the translation object
932
for a given language, installing it as the translation object for the
933
current thread and reinstall the previous active language on exit.
934
Optionally it can simply deinstall the temporary translation on exit with
935
:func:`django.utils.translation.deactivate` if the deactivate argument is
936
True. If you pass None as the language argument, a NullTranslations()
937
instance is installed while the context is active.
939
.. function:: get_language()
941
Returns the currently selected language code.
943
.. function:: get_language_bidi()
945
Returns selected language's BiDi layout:
947
* ``False`` = left-to-right layout
948
* ``True`` = right-to-left layout
950
.. function:: get_language_from_request(request, check_path=False)
952
Analyzes the request to find what language the user wants the system to show.
953
Only languages listed in settings.LANGUAGES are taken into account. If the user
954
requests a sublanguage where we have a main language, we send out the main
957
If ``check_path`` is ``True``, the function first checks the requested URL
958
for whether its path begins with a language code listed in the
959
:setting:`LANGUAGES` setting.
961
.. function:: to_locale(language)
963
Turns a language name (en-us) into a locale name (en_US).
965
.. function:: templatize(src)
967
Turns a Django template into something that is understood by xgettext. It does
968
so by translating the Django translation tags into standard gettext function
828
If ``value`` is ``"Joel is a slug"``, the output will be
829
``"joel-is-a-slug"``.
971
831
.. _time-zone-selection-functions:
1074
943
.. _pytz: http://pytz.sourceforge.net/
945
``django.utils.translation``
946
============================
948
.. module:: django.utils.translation
949
:synopsis: Internationalization support.
951
For a complete discussion on the usage of the following see the
952
:doc:`translation documentation </topics/i18n/translation>`.
954
.. function:: gettext(message)
956
Translates ``message`` and returns it in a UTF-8 bytestring
958
.. function:: ugettext(message)
960
Translates ``message`` and returns it in a unicode string
962
.. function:: pgettext(context, message)
964
Translates ``message`` given the ``context`` and returns
965
it in a unicode string.
967
For more information, see :ref:`contextual-markers`.
969
.. function:: gettext_lazy(message)
970
.. function:: ugettext_lazy(message)
971
.. function:: pgettext_lazy(context, message)
973
Same as the non-lazy versions above, but using lazy execution.
975
See :ref:`lazy translations documentation <lazy-translations>`.
977
.. function:: gettext_noop(message)
978
.. function:: ugettext_noop(message)
980
Marks strings for translation but doesn't translate them now. This can be
981
used to store strings in global variables that should stay in the base
982
language (because they might be used externally) and will be translated
985
.. function:: ngettext(singular, plural, number)
987
Translates ``singular`` and ``plural`` and returns the appropriate string
988
based on ``number`` in a UTF-8 bytestring.
990
.. function:: ungettext(singular, plural, number)
992
Translates ``singular`` and ``plural`` and returns the appropriate string
993
based on ``number`` in a unicode string.
995
.. function:: npgettext(context, singular, plural, number)
997
Translates ``singular`` and ``plural`` and returns the appropriate string
998
based on ``number`` and the ``context`` in a unicode string.
1000
.. function:: ngettext_lazy(singular, plural, number)
1001
.. function:: ungettext_lazy(singular, plural, number)
1002
.. function:: npgettext_lazy(context, singular, plural, number)
1004
Same as the non-lazy versions above, but using lazy execution.
1006
See :ref:`lazy translations documentation <lazy-translations>`.
1008
.. function:: string_concat(*strings)
1010
Lazy variant of string concatenation, needed for translations that are
1011
constructed from multiple parts.
1013
.. function:: activate(language)
1015
Fetches the translation object for a given language and activates it as
1016
the current translation object for the current thread.
1018
.. function:: deactivate()
1020
Deactivates the currently active translation object so that further _ calls
1021
will resolve against the default translation object, again.
1023
.. function:: deactivate_all()
1025
Makes the active translation object a ``NullTranslations()`` instance.
1026
This is useful when we want delayed translations to appear as the original
1027
string for some reason.
1029
.. function:: override(language, deactivate=False)
1031
A Python context manager that uses
1032
:func:`django.utils.translation.activate` to fetch the translation object
1033
for a given language, activates it as the translation object for the
1034
current thread and reactivates the previous active language on exit.
1035
Optionally, it can simply deactivate the temporary translation on exit with
1036
:func:`django.utils.translation.deactivate` if the ``deactivate`` argument
1037
is ``True``. If you pass ``None`` as the language argument, a
1038
``NullTranslations()`` instance is activated within the context.
1040
.. function:: get_language()
1042
Returns the currently selected language code.
1044
.. function:: get_language_bidi()
1046
Returns selected language's BiDi layout:
1048
* ``False`` = left-to-right layout
1049
* ``True`` = right-to-left layout
1051
.. function:: get_language_from_request(request, check_path=False)
1053
Analyzes the request to find what language the user wants the system to
1054
show. Only languages listed in settings.LANGUAGES are taken into account.
1055
If the user requests a sublanguage where we have a main language, we send
1056
out the main language.
1058
If ``check_path`` is ``True``, the function first checks the requested URL
1059
for whether its path begins with a language code listed in the
1060
:setting:`LANGUAGES` setting.
1062
.. function:: to_locale(language)
1064
Turns a language name (en-us) into a locale name (en_US).
1066
.. function:: templatize(src)
1068
Turns a Django template into something that is understood by ``xgettext``.
1069
It does so by translating the Django translation tags into standard
1070
``gettext`` function invocations.
1072
.. data:: LANGUAGE_SESSION_KEY
1074
Session key under which the active language for the current session is
1076
1077
``django.utils.tzinfo``
1077
1078
=======================
1081
Use :mod:`~django.utils.timezone` instead.
1079
1083
.. module:: django.utils.tzinfo
1080
1084
:synopsis: Implementation of ``tzinfo`` classes for use with ``datetime.datetime``.