title: How do I construct a Django reverse/url using query args?
body: <p>I have URLs like <a href="http://example.com/depict?smiles=CO&amp;width=200&amp;height=200" rel="noreferrer">http://example.com/depict?smiles=CO&amp;width=200&amp;height=200</a> (and with several other optional arguments)</p>
  
  <p>My urls.py contains:</p>
  
  <pre><code>urlpatterns = patterns('',
      (r'^$', 'cansmi.index'),
      (r'^cansmi$', 'cansmi.cansmi'),
      url(r'^depict$', cyclops.django.depict, name="cyclops-depict"),
  </code></pre>
  
  <p>I can go to that URL and get the 200x200 PNG that was constructed, so I know that part works.</p>
  
  <p>In my template from the "cansmi.cansmi" response I want to construct a URL for the named template "cyclops-depict" given some query parameters. I thought I could do</p>
  
  <blockquote>
    <p>{% url cyclops-depict smiles=input_smiles width=200 height=200 %}</p>
  </blockquote>
  
  <p>where "input_smiles" is an input to the template via a form submission. In this case it's the string "CO" and I thought it would create a URL like the one at top.</p>
  
  <p>This template fails with a TemplateSyntaxError:</p>
  
  <blockquote>
    <p>Caught an exception while rendering: Reverse for 'cyclops-depict' with arguments '()' and keyword arguments '{'smiles': u'CO', 'height': 200, 'width': 200}' not found.</p>
  </blockquote>
  
  <p>This is a rather common error message both here on StackOverflow and elsewhere. In every case I found, people were using them with parameters in the URL path regexp, which is not the case I have where the parameters go into the query.</p>
  
  <p>That means I'm doing it wrong. How do I do it right? That is, I want to construct the full URL, including path and query parameters, using something in the template.</p>
  
  <p>For reference,</p>
  
  <pre><code>% python manage.py shell
  Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  (InteractiveConsole)
  &gt;&gt;&gt; from django.core.urlresolvers import reverse
  &gt;&gt;&gt; reverse("cyclops-depict", kwargs=dict())
  '/depict'
  &gt;&gt;&gt; reverse("cyclops-depict", kwargs=dict(smiles="CO"))
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/Library/Python/2.6/site-packages/django/core/urlresolvers.py", line 356, in reverse
      *args, **kwargs)))
    File "/Library/Python/2.6/site-packages/django/core/urlresolvers.py", line 302, in reverse
      "arguments '%s' not found." % (lookup_view_s, args, kwargs))
  NoReverseMatch: Reverse for 'cyclops-depict' with arguments '()' and keyword arguments '{'smiles': 'CO'}' not found.
  </code></pre>