
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="id">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.views.static &#8212; dokumentasi Django 2.2.9.dev20191202104323</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../_static/translations.js"></script>
    <link rel="index" title="Indeks" href="../../../genindex.html" />
    <link rel="search" title="Pencarian" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(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">dokumentasi Django 2.2.9.dev20191202104323</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="Kode modul" 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-static">
            
  <h1>Kode sumber untuk django.views.static</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Views and functions for serving static files. These are only to be used</span>
<span class="sd">during development, and SHOULD NOT be used in a production setting.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="kn">import</span> <span class="nn">posixpath</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="k">import</span> <span class="n">Path</span>

<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">FileResponse</span><span class="p">,</span> <span class="n">Http404</span><span class="p">,</span> <span class="n">HttpResponse</span><span class="p">,</span> <span class="n">HttpResponseNotModified</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="k">import</span> <span class="n">Context</span><span class="p">,</span> <span class="n">Engine</span><span class="p">,</span> <span class="n">TemplateDoesNotExist</span><span class="p">,</span> <span class="n">loader</span>
<span class="kn">from</span> <span class="nn">django.utils._os</span> <span class="k">import</span> <span class="n">safe_join</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="k">import</span> <span class="n">http_date</span><span class="p">,</span> <span class="n">parse_http_date</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">gettext</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">gettext_lazy</span>


<span class="k">def</span> <span class="nf">serve</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">document_root</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">show_indexes</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Serve static files below a given point in the directory structure.</span>

<span class="sd">    To use, put a URL pattern such as::</span>

<span class="sd">        from django.views.static import serve</span>

<span class="sd">        url(r&#39;^(?P&lt;path&gt;.*)$&#39;, serve, {&#39;document_root&#39;: &#39;/path/to/my/files/&#39;})</span>

<span class="sd">    in your URLconf. You must provide the ``document_root`` param. You may</span>
<span class="sd">    also set ``show_indexes`` to ``True`` if you&#39;d like to serve a basic index</span>
<span class="sd">    of the directory.  This index view will use the template hardcoded below,</span>
<span class="sd">    but if you&#39;d like to override it, you can create a template called</span>
<span class="sd">    ``static/directory_index.html``.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">path</span> <span class="o">=</span> <span class="n">posixpath</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
    <span class="n">fullpath</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">safe_join</span><span class="p">(</span><span class="n">document_root</span><span class="p">,</span> <span class="n">path</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">fullpath</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">show_indexes</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">directory_index</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">fullpath</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;Directory indexes are not allowed here.&quot;</span><span class="p">))</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">fullpath</span><span class="o">.</span><span class="n">exists</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="s1">&#39;&quot;</span><span class="si">%(path)s</span><span class="s1">&quot; does not exist&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;path&#39;</span><span class="p">:</span> <span class="n">fullpath</span><span class="p">})</span>
    <span class="c1"># Respect the If-Modified-Since header.</span>
    <span class="n">statobj</span> <span class="o">=</span> <span class="n">fullpath</span><span class="o">.</span><span class="n">stat</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">was_modified_since</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;HTTP_IF_MODIFIED_SINCE&#39;</span><span class="p">),</span>
                              <span class="n">statobj</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">,</span> <span class="n">statobj</span><span class="o">.</span><span class="n">st_size</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseNotModified</span><span class="p">()</span>
    <span class="n">content_type</span><span class="p">,</span> <span class="n">encoding</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">fullpath</span><span class="p">))</span>
    <span class="n">content_type</span> <span class="o">=</span> <span class="n">content_type</span> <span class="ow">or</span> <span class="s1">&#39;application/octet-stream&#39;</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">FileResponse</span><span class="p">(</span><span class="n">fullpath</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;rb&#39;</span><span class="p">),</span> <span class="n">content_type</span><span class="o">=</span><span class="n">content_type</span><span class="p">)</span>
    <span class="n">response</span><span class="p">[</span><span class="s2">&quot;Last-Modified&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">http_date</span><span class="p">(</span><span class="n">statobj</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">encoding</span><span class="p">:</span>
        <span class="n">response</span><span class="p">[</span><span class="s2">&quot;Content-Encoding&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">encoding</span>
    <span class="k">return</span> <span class="n">response</span>


<span class="n">DEFAULT_DIRECTORY_INDEX_TEMPLATE</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">{</span><span class="si">% lo</span><span class="s2">ad i18n %}</span>
<span class="s2">&lt;!DOCTYPE html&gt;</span>
<span class="s2">&lt;html lang=&quot;en&quot;&gt;</span>
<span class="s2">  &lt;head&gt;</span>
<span class="s2">    &lt;meta http-equiv=&quot;Content-type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;</span>
<span class="s2">    &lt;meta http-equiv=&quot;Content-Language&quot; content=&quot;en-us&quot;&gt;</span>
<span class="s2">    &lt;meta name=&quot;robots&quot; content=&quot;NONE,NOARCHIVE&quot;&gt;</span>
<span class="s2">    &lt;title&gt;{% blocktrans %}Index of {{ directory }}{</span><span class="si">% e</span><span class="s2">ndblocktrans %}&lt;/title&gt;</span>
<span class="s2">  &lt;/head&gt;</span>
<span class="s2">  &lt;body&gt;</span>
<span class="s2">    &lt;h1&gt;{% blocktrans %}Index of {{ directory }}{</span><span class="si">% e</span><span class="s2">ndblocktrans %}&lt;/h1&gt;</span>
<span class="s2">    &lt;ul&gt;</span>
<span class="s2">      {</span><span class="si">% i</span><span class="s2">f directory != &quot;/&quot; %}</span>
<span class="s2">      &lt;li&gt;&lt;a href=&quot;../&quot;&gt;../&lt;/a&gt;&lt;/li&gt;</span>
<span class="s2">      {</span><span class="si">% e</span><span class="s2">ndif %}</span>
<span class="s2">      {</span><span class="si">% f</span><span class="s2">or f in file_list %}</span>
<span class="s2">      &lt;li&gt;&lt;a href=&quot;{{ f|urlencode }}&quot;&gt;{{ f }}&lt;/a&gt;&lt;/li&gt;</span>
<span class="s2">      {</span><span class="si">% e</span><span class="s2">ndfor %}</span>
<span class="s2">    &lt;/ul&gt;</span>
<span class="s2">  &lt;/body&gt;</span>
<span class="s2">&lt;/html&gt;</span>
<span class="s2">&quot;&quot;&quot;</span>
<span class="n">template_translatable</span> <span class="o">=</span> <span class="n">gettext_lazy</span><span class="p">(</span><span class="s2">&quot;Index of </span><span class="si">%(directory)s</span><span class="s2">&quot;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">directory_index</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">fullpath</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">loader</span><span class="o">.</span><span class="n">select_template</span><span class="p">([</span>
            <span class="s1">&#39;static/directory_index.html&#39;</span><span class="p">,</span>
            <span class="s1">&#39;static/directory_index&#39;</span><span class="p">,</span>
        <span class="p">])</span>
    <span class="k">except</span> <span class="n">TemplateDoesNotExist</span><span class="p">:</span>
        <span class="n">t</span> <span class="o">=</span> <span class="n">Engine</span><span class="p">(</span><span class="n">libraries</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;i18n&#39;</span><span class="p">:</span> <span class="s1">&#39;django.templatetags.i18n&#39;</span><span class="p">})</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">DEFAULT_DIRECTORY_INDEX_TEMPLATE</span><span class="p">)</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">Context</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">c</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fullpath</span><span class="o">.</span><span class="n">iterdir</span><span class="p">():</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="n">url</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">relative_to</span><span class="p">(</span><span class="n">fullpath</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">f</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
                <span class="n">url</span> <span class="o">+=</span> <span class="s1">&#39;/&#39;</span>
            <span class="n">files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
    <span class="n">c</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
        <span class="s1">&#39;directory&#39;</span><span class="p">:</span> <span class="n">path</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span>
        <span class="s1">&#39;file_list&#39;</span><span class="p">:</span> <span class="n">files</span><span class="p">,</span>
    <span class="p">})</span>
    <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">was_modified_since</span><span class="p">(</span><span class="n">header</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">mtime</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Was something modified since the user last downloaded it?</span>

<span class="sd">    header</span>
<span class="sd">      This is the value of the If-Modified-Since header.  If this is None,</span>
<span class="sd">      I&#39;ll just return True.</span>

<span class="sd">    mtime</span>
<span class="sd">      This is the modification time of the item we&#39;re talking about.</span>

<span class="sd">    size</span>
<span class="sd">      This is the size of the item we&#39;re talking about.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">header</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span>
        <span class="n">matches</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;^([^;]+)(; length=([0-9]+))?$&quot;</span><span class="p">,</span> <span class="n">header</span><span class="p">,</span>
                           <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
        <span class="n">header_mtime</span> <span class="o">=</span> <span class="n">parse_http_date</span><span class="p">(</span><span class="n">matches</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
        <span class="n">header_len</span> <span class="o">=</span> <span class="n">matches</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">header_len</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">header_len</span><span class="p">)</span> <span class="o">!=</span> <span class="n">size</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span>
        <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">mtime</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">header_mtime</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="ne">OverflowError</span><span class="p">):</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Pencarian cepat</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Des 02, 2019</p>
          </div>
        
      
    </div>

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

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