<!DOCTYPE html>

<html lang="en" data-content_root="../../../../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>django.views.generic.dates &#8212; Django 5.2 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css?v=34bb78ad" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/default.css?v=bf4d74af" />
    <script src="../../../../_static/documentation_options.js?v=6593ad68"></script>
    <script src="../../../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script src="../../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 5.2 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-views-generic-dates">
            
  <h1>Source code for django.views.generic.dates</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span><span class="w"> </span><span class="nn">datetime</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">django.conf</span><span class="w"> </span><span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.core.exceptions</span><span class="w"> </span><span class="kn">import</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.db</span><span class="w"> </span><span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.http</span><span class="w"> </span><span class="kn">import</span> <span class="n">Http404</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils</span><span class="w"> </span><span class="kn">import</span> <span class="n">timezone</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.functional</span><span class="w"> </span><span class="kn">import</span> <span class="n">cached_property</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.utils.translation</span><span class="w"> </span><span class="kn">import</span> <span class="n">gettext</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.generic.base</span><span class="w"> </span><span class="kn">import</span> <span class="n">View</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.generic.detail</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
    <span class="n">BaseDetailView</span><span class="p">,</span>
    <span class="n">SingleObjectTemplateResponseMixin</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">django.views.generic.list</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
    <span class="n">MultipleObjectMixin</span><span class="p">,</span>
    <span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span>
<span class="p">)</span>


<div class="viewcode-block" id="YearMixin">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.YearMixin">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">YearMixin</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Mixin for views manipulating year-based data.&quot;&quot;&quot;</span>

    <span class="n">year_format</span> <span class="o">=</span> <span class="s2">&quot;%Y&quot;</span>
    <span class="n">year</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="YearMixin.get_year_format">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.YearMixin.get_year_format">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_year_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a year format string in strptime syntax to be used to parse the</span>
<span class="sd">        year from url variables.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_format</span></div>


<div class="viewcode-block" id="YearMixin.get_year">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.YearMixin.get_year">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_year</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the year for which this view should display data.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">year</span>
        <span class="k">if</span> <span class="n">year</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;year&quot;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s2">&quot;year&quot;</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;No year specified&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">year</span></div>


<div class="viewcode-block" id="YearMixin.get_next_year">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.YearMixin.get_next_year">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_next_year</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the next valid year.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;year&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="YearMixin.get_previous_year">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.YearMixin.get_previous_year">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_previous_year</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the previous valid year.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;year&quot;</span><span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_get_next_year</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the start date of the next interval.</span>

<span class="sd">        The interval is defined by start date &lt;= item date &lt; next start date.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">date</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;Date out of range&quot;</span><span class="p">))</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_current_year</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the start date of the current interval.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span></div>



<div class="viewcode-block" id="MonthMixin">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.MonthMixin">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">MonthMixin</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Mixin for views manipulating month-based data.&quot;&quot;&quot;</span>

    <span class="n">month_format</span> <span class="o">=</span> <span class="s2">&quot;%b&quot;</span>
    <span class="n">month</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="MonthMixin.get_month_format">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.MonthMixin.get_month_format">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_month_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a month format string in strptime syntax to be used to parse the</span>
<span class="sd">        month from url variables.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">month_format</span></div>


<div class="viewcode-block" id="MonthMixin.get_month">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.MonthMixin.get_month">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_month</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the month for which this view should display data.&quot;&quot;&quot;</span>
        <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">month</span>
        <span class="k">if</span> <span class="n">month</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;month&quot;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s2">&quot;month&quot;</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;No month specified&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">month</span></div>


<div class="viewcode-block" id="MonthMixin.get_next_month">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.MonthMixin.get_next_month">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_next_month</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the next valid month.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;month&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="MonthMixin.get_previous_month">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.MonthMixin.get_previous_month">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_previous_month</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the previous valid month.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;month&quot;</span><span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_get_next_month</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the start date of the next interval.</span>

<span class="sd">        The interval is defined by start date &lt;= item date &lt; next start date.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">date</span><span class="o">.</span><span class="n">month</span> <span class="o">==</span> <span class="mi">12</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="n">date</span><span class="o">.</span><span class="n">year</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;Date out of range&quot;</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">month</span><span class="o">=</span><span class="n">date</span><span class="o">.</span><span class="n">month</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_current_month</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the start date of the previous interval.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">day</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span></div>



<div class="viewcode-block" id="DayMixin">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DayMixin">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">DayMixin</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Mixin for views manipulating day-based data.&quot;&quot;&quot;</span>

    <span class="n">day_format</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span>
    <span class="n">day</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="DayMixin.get_day_format">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DayMixin.get_day_format">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_day_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a day format string in strptime syntax to be used to parse the day</span>
<span class="sd">        from url variables.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">day_format</span></div>


<div class="viewcode-block" id="DayMixin.get_day">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DayMixin.get_day">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_day</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the day for which this view should display data.&quot;&quot;&quot;</span>
        <span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">day</span>
        <span class="k">if</span> <span class="n">day</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;day&quot;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s2">&quot;day&quot;</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;No day specified&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">day</span></div>


<div class="viewcode-block" id="DayMixin.get_next_day">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DayMixin.get_next_day">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_next_day</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the next valid day.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;day&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="DayMixin.get_previous_day">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DayMixin.get_previous_day">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_previous_day</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the previous valid day.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;day&quot;</span><span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_get_next_day</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the start date of the next interval.</span>

<span class="sd">        The interval is defined by start date &lt;= item date &lt; next start date.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">date</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_current_day</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the start date of the current interval.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">date</span></div>



<div class="viewcode-block" id="WeekMixin">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.WeekMixin">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">WeekMixin</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Mixin for views manipulating week-based data.&quot;&quot;&quot;</span>

    <span class="n">week_format</span> <span class="o">=</span> <span class="s2">&quot;%U&quot;</span>
    <span class="n">week</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="WeekMixin.get_week_format">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.WeekMixin.get_week_format">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_week_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a week format string in strptime syntax to be used to parse the</span>
<span class="sd">        week from url variables.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">week_format</span></div>


<div class="viewcode-block" id="WeekMixin.get_week">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.WeekMixin.get_week">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_week</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the week for which this view should display data.&quot;&quot;&quot;</span>
        <span class="n">week</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">week</span>
        <span class="k">if</span> <span class="n">week</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">week</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;week&quot;</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">week</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s2">&quot;week&quot;</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;No week specified&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">week</span></div>


<div class="viewcode-block" id="WeekMixin.get_next_week">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.WeekMixin.get_next_week">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_next_week</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the next valid week.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;week&quot;</span><span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">get_previous_week</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the previous valid week.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">_get_next_prev</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">period</span><span class="o">=</span><span class="s2">&quot;week&quot;</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_next_week</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the start date of the next interval.</span>

<span class="sd">        The interval is defined by start date &lt;= item date &lt; next start date.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">date</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">7</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_weekday</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>
        <span class="k">except</span> <span class="ne">OverflowError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s2">&quot;Date out of range&quot;</span><span class="p">))</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_current_week</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the start date of the current interval.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">date</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_weekday</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_weekday</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the weekday for a given date.</span>

<span class="sd">        The first day according to the week format is 0 and the last day is 6.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">week_format</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_week_format</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">week_format</span> <span class="ow">in</span> <span class="p">{</span><span class="s2">&quot;%W&quot;</span><span class="p">,</span> <span class="s2">&quot;%V&quot;</span><span class="p">}:</span>  <span class="c1"># week starts on Monday</span>
            <span class="k">return</span> <span class="n">date</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">week_format</span> <span class="o">==</span> <span class="s2">&quot;%U&quot;</span><span class="p">:</span>  <span class="c1"># week starts on Sunday</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">date</span><span class="o">.</span><span class="n">weekday</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">7</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;unknown week format: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">week_format</span><span class="p">)</span></div>



<div class="viewcode-block" id="DateMixin">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DateMixin">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">DateMixin</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Mixin class for views manipulating date-based data.&quot;&quot;&quot;</span>

    <span class="n">date_field</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">False</span>

<div class="viewcode-block" id="DateMixin.get_date_field">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DateMixin.get_date_field">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_date_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the name of the date field to be used to filter by.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_field</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.date_field is required.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_field</span></div>


<div class="viewcode-block" id="DateMixin.get_allow_future">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.DateMixin.get_allow_future">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_allow_future</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return `True` if the view should be allowed to display objects from</span>
<span class="sd">        the future.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">allow_future</span></div>


    <span class="c1"># Note: the following three methods only work in subclasses that also</span>
    <span class="c1"># inherit SingleObjectMixin or MultipleObjectMixin.</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">uses_datetime_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return `True` if the date field is a `DateTimeField` and `False`</span>
<span class="sd">        if it&#39;s a `DateField`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">model</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="n">field</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">())</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_make_date_lookup_arg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convert a date into a datetime when the date field is a DateTimeField.</span>

<span class="sd">        When time zone support is enabled, `date` is assumed to be in the</span>
<span class="sd">        current time zone, so that displayed items are consistent with the URL.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uses_datetime_field</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">time</span><span class="o">.</span><span class="n">min</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_TZ</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">make_aware</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_make_single_date_lookup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the lookup kwargs for filtering on a single date.</span>

<span class="sd">        If the date field is a DateTimeField, we can&#39;t just filter on</span>
<span class="sd">        date_field=date because that doesn&#39;t take the time into account.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uses_datetime_field</span><span class="p">:</span>
            <span class="n">since</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
            <span class="n">until</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">date</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
            <span class="k">return</span> <span class="p">{</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__gte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">since</span><span class="p">,</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lt&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">until</span><span class="p">,</span>
            <span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Skip self._make_date_lookup_arg, it&#39;s a no-op in this branch.</span>
            <span class="k">return</span> <span class="p">{</span><span class="n">date_field</span><span class="p">:</span> <span class="n">date</span><span class="p">}</span></div>



<div class="viewcode-block" id="BaseDateListView">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.BaseDateListView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseDateListView</span><span class="p">(</span><span class="n">MultipleObjectMixin</span><span class="p">,</span> <span class="n">DateMixin</span><span class="p">,</span> <span class="n">View</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for date-based views displaying a list of objects.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">allow_empty</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">date_list_period</span> <span class="o">=</span> <span class="s2">&quot;year&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">date_list</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_list</span><span class="p">,</span> <span class="n">extra_context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_items</span><span class="p">()</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span>
            <span class="n">object_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">object_list</span><span class="p">,</span> <span class="n">date_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">date_list</span><span class="p">,</span> <span class="o">**</span><span class="n">extra_context</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_to_response</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>

<div class="viewcode-block" id="BaseDateListView.get_dated_items">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.BaseDateListView.get_dated_items">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Obtain the list of dates and items.&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s2">&quot;A DateView must provide an implementation of get_dated_items()&quot;</span>
        <span class="p">)</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">get_ordering</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the field or fields to use for ordering the queryset; use the</span>
<span class="sd">        date field by default.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s2">&quot;-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordering</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordering</span>

<div class="viewcode-block" id="BaseDateListView.get_dated_queryset">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.BaseDateListView.get_dated_queryset">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">lookup</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a queryset properly filtered according to `allow_future` and any</span>
<span class="sd">        extra lookup kwargs.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">lookup</span><span class="p">)</span>
        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="n">allow_future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_allow_future</span><span class="p">()</span>
        <span class="n">allow_empty</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_allow_empty</span><span class="p">()</span>
        <span class="n">paginate_by</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_paginate_by</span><span class="p">(</span><span class="n">qs</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">allow_future</span><span class="p">:</span>
            <span class="n">now</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uses_datetime_field</span> <span class="k">else</span> <span class="n">timezone_today</span><span class="p">()</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">now</span><span class="p">})</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">allow_empty</span><span class="p">:</span>
            <span class="c1"># When pagination is enabled, it&#39;s better to do a cheap query</span>
            <span class="c1"># than to load the unpaginated queryset in memory.</span>
            <span class="n">is_empty</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">qs</span> <span class="k">if</span> <span class="n">paginate_by</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="ow">not</span> <span class="n">qs</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">is_empty</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span>
                    <span class="n">_</span><span class="p">(</span><span class="s2">&quot;No </span><span class="si">%(verbose_name_plural)s</span><span class="s2"> available&quot;</span><span class="p">)</span>
                    <span class="o">%</span> <span class="p">{</span>
                        <span class="s2">&quot;verbose_name_plural&quot;</span><span class="p">:</span> <span class="n">qs</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">,</span>
                    <span class="p">}</span>
                <span class="p">)</span>

        <span class="k">return</span> <span class="n">qs</span></div>


<div class="viewcode-block" id="BaseDateListView.get_date_list_period">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.BaseDateListView.get_date_list_period">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_date_list_period</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the aggregation period for the list of dates: &#39;year&#39;, &#39;month&#39;, or</span>
<span class="sd">        &#39;day&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_list_period</span></div>


<div class="viewcode-block" id="BaseDateListView.get_date_list">
<a class="viewcode-back" href="../../../../ref/class-based-views/mixins-date-based.html#django.views.generic.dates.BaseDateListView.get_date_list">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">get_date_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">date_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ordering</span><span class="o">=</span><span class="s2">&quot;ASC&quot;</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a date list by calling `queryset.dates/datetimes()`, checking</span>
<span class="sd">        along the way for empty lists that aren&#39;t allowed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="n">allow_empty</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_allow_empty</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">date_type</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">date_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_list_period</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">uses_datetime_field</span><span class="p">:</span>
            <span class="n">date_list</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">datetimes</span><span class="p">(</span><span class="n">date_field</span><span class="p">,</span> <span class="n">date_type</span><span class="p">,</span> <span class="n">ordering</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">date_list</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">dates</span><span class="p">(</span><span class="n">date_field</span><span class="p">,</span> <span class="n">date_type</span><span class="p">,</span> <span class="n">ordering</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">date_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">date_list</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">allow_empty</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s2">&quot;No </span><span class="si">%(verbose_name_plural)s</span><span class="s2"> available&quot;</span><span class="p">)</span>
                <span class="o">%</span> <span class="p">{</span>
                    <span class="s2">&quot;verbose_name_plural&quot;</span><span class="p">:</span> <span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">,</span>
                <span class="p">}</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">date_list</span></div>
</div>



<div class="viewcode-block" id="BaseArchiveIndexView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseArchiveIndexView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseArchiveIndexView</span><span class="p">(</span><span class="n">BaseDateListView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for archives of date-based items.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">context_object_name</span> <span class="o">=</span> <span class="s2">&quot;latest&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_queryset</span><span class="p">()</span>
        <span class="n">date_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_list</span><span class="p">(</span><span class="n">qs</span><span class="p">,</span> <span class="n">ordering</span><span class="o">=</span><span class="s2">&quot;DESC&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">date_list</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>

        <span class="k">return</span> <span class="p">(</span><span class="n">date_list</span><span class="p">,</span> <span class="n">qs</span><span class="p">,</span> <span class="p">{})</span></div>



<div class="viewcode-block" id="ArchiveIndexView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.ArchiveIndexView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">ArchiveIndexView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseArchiveIndexView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Top-level archive of date-based items.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive&quot;</span></div>



<div class="viewcode-block" id="BaseYearArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseYearArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseYearArchiveView</span><span class="p">(</span><span class="n">YearMixin</span><span class="p">,</span> <span class="n">BaseDateListView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for a list of objects published in a given year.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">date_list_period</span> <span class="o">=</span> <span class="s2">&quot;month&quot;</span>
    <span class="n">make_object_list</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year</span><span class="p">()</span>

        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="n">date</span> <span class="o">=</span> <span class="n">_date_from_string</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year_format</span><span class="p">())</span>

        <span class="n">since</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">until</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_next_year</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>
        <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__gte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">since</span><span class="p">,</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lt&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">until</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_queryset</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>
        <span class="n">date_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_list</span><span class="p">(</span><span class="n">qs</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_make_object_list</span><span class="p">():</span>
            <span class="c1"># We need this to be a queryset since parent classes introspect it</span>
            <span class="c1"># to find information about the model.</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">none</span><span class="p">()</span>

        <span class="k">return</span> <span class="p">(</span>
            <span class="n">date_list</span><span class="p">,</span>
            <span class="n">qs</span><span class="p">,</span>
            <span class="p">{</span>
                <span class="s2">&quot;year&quot;</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span>
                <span class="s2">&quot;next_year&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_year</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;previous_year&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_previous_year</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
            <span class="p">},</span>
        <span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_make_object_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return `True` if this view should contain the full list of objects in</span>
<span class="sd">        the given year.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">make_object_list</span></div>



<div class="viewcode-block" id="YearArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.YearArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">YearArchiveView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseYearArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;List of objects published in a given year.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive_year&quot;</span></div>



<div class="viewcode-block" id="BaseMonthArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseMonthArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseMonthArchiveView</span><span class="p">(</span><span class="n">YearMixin</span><span class="p">,</span> <span class="n">MonthMixin</span><span class="p">,</span> <span class="n">BaseDateListView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for a list of objects published in a given month.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">date_list_period</span> <span class="o">=</span> <span class="s2">&quot;day&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year</span><span class="p">()</span>
        <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_month</span><span class="p">()</span>

        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="n">date</span> <span class="o">=</span> <span class="n">_date_from_string</span><span class="p">(</span>
            <span class="n">year</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year_format</span><span class="p">(),</span> <span class="n">month</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_month_format</span><span class="p">()</span>
        <span class="p">)</span>

        <span class="n">since</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">until</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_next_month</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>
        <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__gte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">since</span><span class="p">,</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lt&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">until</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_queryset</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>
        <span class="n">date_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_list</span><span class="p">(</span><span class="n">qs</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">(</span>
            <span class="n">date_list</span><span class="p">,</span>
            <span class="n">qs</span><span class="p">,</span>
            <span class="p">{</span>
                <span class="s2">&quot;month&quot;</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span>
                <span class="s2">&quot;next_month&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_month</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;previous_month&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_previous_month</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
            <span class="p">},</span>
        <span class="p">)</span></div>



<div class="viewcode-block" id="MonthArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.MonthArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">MonthArchiveView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseMonthArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;List of objects published in a given month.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive_month&quot;</span></div>



<div class="viewcode-block" id="BaseWeekArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseWeekArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseWeekArchiveView</span><span class="p">(</span><span class="n">YearMixin</span><span class="p">,</span> <span class="n">WeekMixin</span><span class="p">,</span> <span class="n">BaseDateListView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for a list of objects published in a given week.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year</span><span class="p">()</span>
        <span class="n">week</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_week</span><span class="p">()</span>

        <span class="n">date_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
        <span class="n">week_format</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_week_format</span><span class="p">()</span>
        <span class="n">week_choices</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;%W&quot;</span><span class="p">:</span> <span class="s2">&quot;1&quot;</span><span class="p">,</span> <span class="s2">&quot;%U&quot;</span><span class="p">:</span> <span class="s2">&quot;0&quot;</span><span class="p">,</span> <span class="s2">&quot;%V&quot;</span><span class="p">:</span> <span class="s2">&quot;1&quot;</span><span class="p">}</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">week_start</span> <span class="o">=</span> <span class="n">week_choices</span><span class="p">[</span><span class="n">week_format</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Unknown week format </span><span class="si">%r</span><span class="s2">. Choices are: </span><span class="si">%s</span><span class="s2">&quot;</span>
                <span class="o">%</span> <span class="p">(</span>
                    <span class="n">week_format</span><span class="p">,</span>
                    <span class="s2">&quot;, &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">week_choices</span><span class="p">)),</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="n">year_format</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year_format</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">week_format</span> <span class="o">==</span> <span class="s2">&quot;%V&quot;</span> <span class="ow">and</span> <span class="n">year_format</span> <span class="o">!=</span> <span class="s2">&quot;</span><span class="si">%G</span><span class="s2">&quot;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;ISO week directive &#39;</span><span class="si">%s</span><span class="s2">&#39; is incompatible with the year &quot;</span>
                <span class="s2">&quot;directive &#39;</span><span class="si">%s</span><span class="s2">&#39;. Use the ISO year &#39;</span><span class="si">%%</span><span class="s2">G&#39; instead.&quot;</span>
                <span class="o">%</span> <span class="p">(</span>
                    <span class="n">week_format</span><span class="p">,</span>
                    <span class="n">year_format</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="n">date</span> <span class="o">=</span> <span class="n">_date_from_string</span><span class="p">(</span><span class="n">year</span><span class="p">,</span> <span class="n">year_format</span><span class="p">,</span> <span class="n">week_start</span><span class="p">,</span> <span class="s2">&quot;%w&quot;</span><span class="p">,</span> <span class="n">week</span><span class="p">,</span> <span class="n">week_format</span><span class="p">)</span>
        <span class="n">since</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">until</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_next_week</span><span class="p">(</span><span class="n">date</span><span class="p">))</span>
        <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__gte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">since</span><span class="p">,</span>
            <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lt&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">until</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_queryset</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">(</span>
            <span class="kc">None</span><span class="p">,</span>
            <span class="n">qs</span><span class="p">,</span>
            <span class="p">{</span>
                <span class="s2">&quot;week&quot;</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span>
                <span class="s2">&quot;next_week&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_week</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;previous_week&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_previous_week</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
            <span class="p">},</span>
        <span class="p">)</span></div>



<div class="viewcode-block" id="WeekArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.WeekArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">WeekArchiveView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseWeekArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;List of objects published in a given week.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive_week&quot;</span></div>



<div class="viewcode-block" id="BaseDayArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseDayArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseDayArchiveView</span><span class="p">(</span><span class="n">YearMixin</span><span class="p">,</span> <span class="n">MonthMixin</span><span class="p">,</span> <span class="n">DayMixin</span><span class="p">,</span> <span class="n">BaseDateListView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for a list of objects published on a given day.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year</span><span class="p">()</span>
        <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_month</span><span class="p">()</span>
        <span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_day</span><span class="p">()</span>

        <span class="n">date</span> <span class="o">=</span> <span class="n">_date_from_string</span><span class="p">(</span>
            <span class="n">year</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_year_format</span><span class="p">(),</span>
            <span class="n">month</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_month_format</span><span class="p">(),</span>
            <span class="n">day</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_day_format</span><span class="p">(),</span>
        <span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_dated_items</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">date</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do the actual heavy lifting of getting the dated items; this accepts a</span>
<span class="sd">        date object so that TodayArchiveView can be trivial.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_single_date_lookup</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dated_queryset</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">(</span>
            <span class="kc">None</span><span class="p">,</span>
            <span class="n">qs</span><span class="p">,</span>
            <span class="p">{</span>
                <span class="s2">&quot;day&quot;</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span>
                <span class="s2">&quot;previous_day&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_previous_day</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;next_day&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_day</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;previous_month&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_previous_month</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
                <span class="s2">&quot;next_month&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_month</span><span class="p">(</span><span class="n">date</span><span class="p">),</span>
            <span class="p">},</span>
        <span class="p">)</span></div>



<div class="viewcode-block" id="DayArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.DayArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">DayArchiveView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseDayArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;List of objects published on a given day.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive_day&quot;</span></div>



<div class="viewcode-block" id="BaseTodayArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseTodayArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseTodayArchiveView</span><span class="p">(</span><span class="n">BaseDayArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base view for a list of objects published today.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_dated_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return (date_list, items, extra_context) for this request.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_dated_items</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">())</span></div>



<div class="viewcode-block" id="TodayArchiveView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.TodayArchiveView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">TodayArchiveView</span><span class="p">(</span><span class="n">MultipleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseTodayArchiveView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;List of objects published today.&quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_archive_day&quot;</span></div>



<div class="viewcode-block" id="BaseDateDetailView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.BaseDateDetailView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">BaseDateDetailView</span><span class="p">(</span><span class="n">YearMixin</span><span class="p">,</span> <span class="n">MonthMixin</span><span class="p">,</span> <span class="n">DayMixin</span><span class="p">,</span> <span class="n">DateMixin</span><span class="p">,</span> <span class="n">BaseDetailView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base detail view for a single object on a single date; this differs from the</span>
<span class="sd">    standard DetailView by accepting a year/month/day in the URL.</span>

<span class="sd">    This requires subclassing to provide a response mixin.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">get_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Get the object this request displays.&quot;&quot;&quot;</span>
        <span class="n">year</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_year</span><span class="p">()</span>
        <span class="n">month</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_month</span><span class="p">()</span>
        <span class="n">day</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_day</span><span class="p">()</span>
        <span class="n">date</span> <span class="o">=</span> <span class="n">_date_from_string</span><span class="p">(</span>
            <span class="n">year</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_year_format</span><span class="p">(),</span>
            <span class="n">month</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_month_format</span><span class="p">(),</span>
            <span class="n">day</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">get_day_format</span><span class="p">(),</span>
        <span class="p">)</span>

        <span class="c1"># Use a custom queryset if provided</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span> <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">queryset</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_allow_future</span><span class="p">()</span> <span class="ow">and</span> <span class="n">date</span> <span class="o">&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span>
                    <span class="s2">&quot;Future </span><span class="si">%(verbose_name_plural)s</span><span class="s2"> not available because &quot;</span>
                    <span class="s2">&quot;</span><span class="si">%(class_name)s</span><span class="s2">.allow_future is False.&quot;</span>
                <span class="p">)</span>
                <span class="o">%</span> <span class="p">{</span>
                    <span class="s2">&quot;verbose_name_plural&quot;</span><span class="p">:</span> <span class="n">qs</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">verbose_name_plural</span><span class="p">,</span>
                    <span class="s2">&quot;class_name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
                <span class="p">}</span>
            <span class="p">)</span>

        <span class="c1"># Filter down a queryset from self.queryset using the date from the</span>
        <span class="c1"># URL. This&#39;ll get passed as the queryset to DetailView.get_object,</span>
        <span class="c1"># which&#39;ll handle the 404</span>
        <span class="n">lookup_kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_make_single_date_lookup</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">qs</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">lookup_kwargs</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_object</span><span class="p">(</span><span class="n">queryset</span><span class="o">=</span><span class="n">qs</span><span class="p">)</span></div>



<div class="viewcode-block" id="DateDetailView">
<a class="viewcode-back" href="../../../../ref/class-based-views/generic-date-based.html#django.views.generic.dates.DateDetailView">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">DateDetailView</span><span class="p">(</span><span class="n">SingleObjectTemplateResponseMixin</span><span class="p">,</span> <span class="n">BaseDateDetailView</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Detail view of a single object on a single date; this differs from the</span>
<span class="sd">    standard DetailView by accepting a year/month/day in the URL.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">template_name_suffix</span> <span class="o">=</span> <span class="s2">&quot;_detail&quot;</span></div>



<span class="k">def</span><span class="w"> </span><span class="nf">_date_from_string</span><span class="p">(</span>
    <span class="n">year</span><span class="p">,</span> <span class="n">year_format</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">month_format</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">day_format</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">delim</span><span class="o">=</span><span class="s2">&quot;__&quot;</span>
<span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get a datetime.date object given a format string and a year, month, and day</span>
<span class="sd">    (only year is mandatory). Raise a 404 for an invalid date.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">format</span> <span class="o">=</span> <span class="n">year_format</span> <span class="o">+</span> <span class="n">delim</span> <span class="o">+</span> <span class="n">month_format</span> <span class="o">+</span> <span class="n">delim</span> <span class="o">+</span> <span class="n">day_format</span>
    <span class="n">datestr</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">year</span><span class="p">)</span> <span class="o">+</span> <span class="n">delim</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">month</span><span class="p">)</span> <span class="o">+</span> <span class="n">delim</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">day</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">datestr</span><span class="p">,</span> <span class="nb">format</span><span class="p">)</span><span class="o">.</span><span class="n">date</span><span class="p">()</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">Http404</span><span class="p">(</span>
            <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Invalid date string “</span><span class="si">%(datestr)s</span><span class="s2">” given format “</span><span class="si">%(format)s</span><span class="s2">”&quot;</span><span class="p">)</span>
            <span class="o">%</span> <span class="p">{</span>
                <span class="s2">&quot;datestr&quot;</span><span class="p">:</span> <span class="n">datestr</span><span class="p">,</span>
                <span class="s2">&quot;format&quot;</span><span class="p">:</span> <span class="nb">format</span><span class="p">,</span>
            <span class="p">}</span>
        <span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">_get_next_prev</span><span class="p">(</span><span class="n">generic_view</span><span class="p">,</span> <span class="n">date</span><span class="p">,</span> <span class="n">is_previous</span><span class="p">,</span> <span class="n">period</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get the next or the previous valid date. The idea is to allow links on</span>
<span class="sd">    month/day views to never be 404s by never providing a date that&#39;ll be</span>
<span class="sd">    invalid for the given view.</span>

<span class="sd">    This is a bit complicated since it handles different intervals of time,</span>
<span class="sd">    hence the coupling to generic_view.</span>

<span class="sd">    However in essence the logic comes down to:</span>

<span class="sd">        * If allow_empty and allow_future are both true, this is easy: just</span>
<span class="sd">          return the naive result (just the next/previous day/week/month,</span>
<span class="sd">          regardless of object existence.)</span>

<span class="sd">        * If allow_empty is true, allow_future is false, and the naive result</span>
<span class="sd">          isn&#39;t in the future, then return it; otherwise return None.</span>

<span class="sd">        * If allow_empty is false and allow_future is true, return the next</span>
<span class="sd">          date *that contains a valid object*, even if it&#39;s in the future. If</span>
<span class="sd">          there are no next objects, return None.</span>

<span class="sd">        * If allow_empty is false and allow_future is false, return the next</span>
<span class="sd">          date that contains a valid object. If that date is in the future, or</span>
<span class="sd">          if there are no next objects, return None.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">get_date_field</span><span class="p">()</span>
    <span class="n">allow_empty</span> <span class="o">=</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">get_allow_empty</span><span class="p">()</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">get_allow_future</span><span class="p">()</span>

    <span class="n">get_current</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">generic_view</span><span class="p">,</span> <span class="s2">&quot;_get_current_</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">period</span><span class="p">)</span>
    <span class="n">get_next</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">generic_view</span><span class="p">,</span> <span class="s2">&quot;_get_next_</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">period</span><span class="p">)</span>

    <span class="c1"># Bounds of the current interval</span>
    <span class="n">start</span><span class="p">,</span> <span class="n">end</span> <span class="o">=</span> <span class="n">get_current</span><span class="p">(</span><span class="n">date</span><span class="p">),</span> <span class="n">get_next</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>

    <span class="c1"># If allow_empty is True, the naive result will be valid</span>
    <span class="k">if</span> <span class="n">allow_empty</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">is_previous</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">get_current</span><span class="p">(</span><span class="n">start</span> <span class="o">-</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">days</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">end</span>

        <span class="k">if</span> <span class="n">allow_future</span> <span class="ow">or</span> <span class="n">result</span> <span class="o">&lt;=</span> <span class="n">timezone_today</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">result</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

    <span class="c1"># Otherwise, we&#39;ll need to go to the database to look for an object</span>
    <span class="c1"># whose date_field is at least (greater than/less than) the given</span>
    <span class="c1"># naive result</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Construct a lookup and an ordering depending on whether we&#39;re doing</span>
        <span class="c1"># a previous date or a next date lookup.</span>
        <span class="k">if</span> <span class="n">is_previous</span><span class="p">:</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lt&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">start</span><span class="p">)}</span>
            <span class="n">ordering</span> <span class="o">=</span> <span class="s2">&quot;-</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">date_field</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__gte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">:</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">_make_date_lookup_arg</span><span class="p">(</span><span class="n">end</span><span class="p">)}</span>
            <span class="n">ordering</span> <span class="o">=</span> <span class="n">date_field</span>

        <span class="c1"># Filter out objects in the future if appropriate.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">allow_future</span><span class="p">:</span>
            <span class="c1"># Fortunately, to match the implementation of allow_future,</span>
            <span class="c1"># we need __lte, which doesn&#39;t conflict with __lt above.</span>
            <span class="k">if</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">uses_datetime_field</span><span class="p">:</span>
                <span class="n">now</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">now</span> <span class="o">=</span> <span class="n">timezone_today</span><span class="p">()</span>
            <span class="n">lookup</span><span class="p">[</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">__lte&quot;</span> <span class="o">%</span> <span class="n">date_field</span><span class="p">]</span> <span class="o">=</span> <span class="n">now</span>

        <span class="n">qs</span> <span class="o">=</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="n">lookup</span><span class="p">)</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">ordering</span><span class="p">)</span>

        <span class="c1"># Snag the first object from the queryset; if it doesn&#39;t exist that</span>
        <span class="c1"># means there&#39;s no next/previous link available.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">qs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">date_field</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="c1"># Convert datetimes to dates in the current time zone.</span>
        <span class="k">if</span> <span class="n">generic_view</span><span class="o">.</span><span class="n">uses_datetime_field</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_TZ</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">date</span><span class="p">()</span>

        <span class="c1"># Return the first day of the period.</span>
        <span class="k">return</span> <span class="n">get_current</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">timezone_today</span><span class="p">():</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Return the current date in the current time zone.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_TZ</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">timezone</span><span class="o">.</span><span class="n">localdate</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Oct 01, 2025</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>