flask-urls/index.html @ a957c8d5dd05

threesome.vim: Update site.
author Steve Losh <steve@stevelosh.com>
date Wed, 29 Jun 2011 18:14:50 -0400
parents 1b76b2808ee7
children (none)

<!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">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Welcome to flask-urls’ documentation! &mdash; flask-urls v0.9.2 documentation</title>
    <link rel="stylesheet" href="_static/flasky.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.9.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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>
    <link rel="top" title="flask-urls v0.9.2 documentation" href="#" /> 
  </head>
  <body>
  
  
  <div class=indexwrapper>
  


    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="welcome-to-flask-urls-documentation">
<h1>Welcome to flask-urls&#8217; documentation!<a class="headerlink" href="#welcome-to-flask-urls-documentation" title="Permalink to this headline">¶</a></h1>
<p>flask-urls is a collection of useful URL-related functions for <a class="reference external" href="http://flask.pocoo.org/">Flask</a>
applications.</p>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Install flask-urls with <a class="reference external" href="http://pip.openplans.org/">pip</a>:</p>
<div class="highlight-python"><pre>pip install -e 'hg+http://bitbucket.org/sjl/flask-urls@v0.9.2#egg=flask-urls'</pre>
</div>
<p>Prefer <a class="reference external" href="http://git-scm.com/">git</a> to <a class="reference external" href="http://hg-scm.org/">Mercurial</a>?</p>
<div class="highlight-python"><pre>pip install -e 'git+http://github.com/sjl/flask-urls.git@v0.9.2#egg=flask-urls'</pre>
</div>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<p>flask-urls currently provides one function to make dealing with URLs in Flask
applications a bit easier.</p>
<div class="section" id="permalink">
<h3>permalink<a class="headerlink" href="#permalink" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">permalink</span></tt> decorator was taken from <a class="reference external" href="http://flask.pocoo.org/snippets/6/">this snippet</a> on the Flask site.
It&#8217;s used to wrap functions so they only need to return the arguments to
Flask&#8217;s <tt class="docutils literal"><span class="pre">url_for</span></tt> function, instead of calling the function themselves.</p>
<p>For example, say you have several classes that represents items on your site:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">url_for</span>

<span class="k">class</span> <span class="nc">Event</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event_id</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="o">=</span> <span class="n">event_id</span>

    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">url_for</span><span class="p">(</span><span class="s">&#39;event&#39;</span><span class="p">,</span> <span class="n">event_id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">event_id</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>

    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">url_for</span><span class="p">(</span><span class="s">&#39;profile&#39;</span><span class="p">,</span> <span class="n">username</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">username</span><span class="p">)</span>
</pre></div>
</div>
<p>Using the permalink decorator can make the <tt class="docutils literal"><span class="pre">link</span></tt> functions a bit cleaner:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flaskext.urls</span> <span class="kn">import</span> <span class="n">permalink</span>

<span class="k">class</span> <span class="nc">Event</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event_id</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="o">=</span> <span class="n">event_id</span>

    <span class="nd">@permalink</span>
    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;event&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="s">&#39;event_id&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">event_id</span> <span class="p">}</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">username</span>

    <span class="nd">@permalink</span>
    <span class="k">def</span> <span class="nf">link</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;profile&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="s">&#39;username&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">username</span> <span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="contribute">
<h2>Contribute<a class="headerlink" href="#contribute" title="Permalink to this headline">¶</a></h2>
<p>If you want to contribute feel free to fork the <a class="reference external" href="http://bitbucket.org/sjl/flask-urls/">Mercurial repository</a> or <a class="reference external" href="http://github.com/sjl/flask-urls/">git
repository</a> and send a pull request.</p>
</div>
</div>


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

    <div class="footer">Created by <a href="http://stevelosh.com/">Steve Losh</a></p>
    
        </div>
    

  </body>
</html>