<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reversing URL Patterns\n",
    "\n",
    "## ... In Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from django.core.urlresolvers import reverse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse('organizer_tag_list')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse(\n",
    "    'organizer_tag_detail',\n",
    "    args=['tag_slug_string'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse(\n",
    "    'organizer_tag_detail',\n",
    "    kwargs={'slug': 'tag_slug_string'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ... in Templates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from django.template import Template, Context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_list' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_detail' 'tag_slug_string' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_detail' slug='tag_slug_string' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Django Shell-Plus",
   "language": "python",
   "name": "django_extensions"
  },
  "language_info": {<commit_msg>Ch06: Extend code examples in IPython notebook.<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reversing URL Patterns\n",
    "\n",
    "## ... In Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from django.core.urlresolvers import reverse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse('organizer_tag_list')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse(\n",
    "    'organizer_tag_detail',\n",
    "    args=['tag_slug_string'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reverse(\n",
    "    'organizer_tag_detail',\n",
    "    kwargs={'slug': 'tag_slug_string'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ... in Templates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from django.template import Template, Context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_list' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_detail' 'tag_slug_string' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = \"{% url 'organizer_tag_detail' slug='tag_slug_string' %}\"\n",
    "template = Template(code)\n",
    "template.render(Context())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Case for `get_absolute_url()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from django.core.urlresolvers import reverse\n",
    "from organizer.models import Tag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/django/'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "django_tag = Tag.objects.get(slug__iexact='django')\n",
    "reverse(\n",
    "    'organizer_tag_detail',\n",
    "    kwargs={'slug': django_tag.slug})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/tag_slug_string/'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "template_code = (\n",
    "    \"{% url 'organizer_tag_detail' tag.slug %}\")\n",
    "django_tag = Tag.objects.get(slug__iexact='django')\n",
    "context = Context({'tag': django_tag})\n",
    "template = Template(code)\n",
    "template.render(context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/django/'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "django_tag = Tag.objects.get(slug__iexact='django')\n",
    "django_tag.get_absolute_url()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tag/django/'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "django_tag = Tag.objects.get(slug__iexact='django')\n",
    "context = Context({'tag': django_tag})\n",
    "code = \"{{ tag.get_absolute_url }}\"\n",
    "template = Template(code)\n",
    "template.render(context)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `NoReverseMatch`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "from django.core.urlresolvers import (\n",
    "    NoReverseMatch, reverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(\"Reverse for 'no_url_pattern_with_this_name' \"\n",
      " \"with arguments '()' and keyword arguments \"\n",
      " \"'{}' not found. 0 pattern(s) tried: []\")\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    reverse('no_url_pattern_with_this_name')\n",
    "except NoReverseMatch as e:\n",
    "    pprint(str(e), width=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(\"Reverse for 'organizer_tag_detail' with \"\n",
      " \"arguments '()' and keyword arguments '{}' \"\n",
      " 'not found. 1 pattern(s) tried: '\n",
      " \"['tag/(?P<slug>[\\\\\\\\w\\\\\\\\-]+)/$']\")\n"
     ]
    }
   ],
   "source": [
    "# this needs a tag.slug values passed to args or kwargs!\n",
    "try:\n",
    "    reverse('organizer_tag_detail')\n",
    "except NoReverseMatch as e:\n",
    "    pprint(str(e), width=50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Django Shell-Plus",
   "language": "python",
   "name": "django_extensions"
  },
  "language_info": {
{'fraction_non_alphanumeric': 0.2154445165476963, 'fraction_numerical': 0.005469546676555113, 'mean_word_length': 2.1608555523000295, 'pattern_counts': {'":': 262, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '511369', 'n_tokens_mistral': 4166, 'n_tokens_neox': 4062, 'n_words': 620}