djangosnippets.org: Latest snippets tagged with 'reverse'https://djangosnippets.org/tags/reverse/2012-10-19T11:12:48.629453-05:00HTTPS redirections middleware with updated URL template tag
2012-10-19T11:12:48.629453-05:00xlqhttps://djangosnippets.org/snippets/2833/<p>This middleware redirects HTTP requests to HTTPS for some specified URLs, in the same way as <a href="http://djangosnippets.org/snippets/85/">85</a>. It also changes the <code>url</code> template tag to use the <code>https</code> scheme for the same URLs. For example, if you have the following URL pattern:</p>
<pre><code>url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'https': True})
</code></pre>
<p>then the template: …</p>
Freely redistributableReplacing pattern groups by values
2011-08-17T08:26:59.286910-05:00marinhohttps://djangosnippets.org/snippets/2514/<p>This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example:</p>
<p>IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works.</p>
<p>For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'.</p>
<p>For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', …</p>
Freely redistributableUsing reverse with success_url in class based generic views
2011-05-21T18:22:25.126700-05:00cdejanhttps://djangosnippets.org/snippets/2445/<p>If you did tried to use <code>reverse</code> function to set <code>success_url</code> in class based generic views and you have got an exception, this helper function may help.</p>
<p>Put this snipped in some file, for example utils.py and import this function.</p>
Freely redistributableDoing redirect without request
2010-09-23T08:55:10.918970-05:00divermanhttps://djangosnippets.org/snippets/2204/<p>When you neeed to do redirect and request object is not available, you can do it with exception.</p>
<p>Put exception handler somewhere request is available, for example to middleware or ModelAdmin.</p>
<p>Raise exception, where request is not available.</p>
Freely redistributableUsing URLs for highlighting sections in menu
2010-08-09T17:15:18.659869-05:00divermanhttps://djangosnippets.org/snippets/2143/<p>Use this, if you want to "activate" menu items by URL. Typical usage is with CSS class 'active'.</p>
Freely redistributabledjango subdomain support for both resolve and reverse.
2010-06-11T07:55:53.491096-05:00puppyhttps://djangosnippets.org/snippets/2062/<p>Add these two middleware to the top of MIDDLEWARE_CLASSES.
Add BASE_DOMAIN to your setting file : BASE_DOMAIN = '.13.com'.</p>
<p>your root urlconf may like this:
urlpatterns = patterns('',
url(r'^www:(?P<id>[0-9]+)/$', 'couponcn.store.views.site_index', name='site_index'),
url(r'^news:abc/def/$', 'couponcn.store.views.site_index', name='site_index2'),
)</p>
<p>then
{% url site_index id=4 %}<br />
{% url site_index2 %}</p>
<p>in your template or …</p>
Freely redistributableEnsure submitted slugs do not conflict with existing resolvable URLs
2009-06-26T18:17:37.885527-05:00ElfSternberghttps://djangosnippets.org/snippets/1598/<p>This code overrides the existing RegistrationForm in django-registration and adds a new validation step. In this step, the username (my example slug) is compared against all the existing URLs that the application currently resolves and, if it <em>does</em> successfully resolve, throws a validation exception. This indicates that the username chosen …</p>
Freely redistributableDecorator to modify reverse() to render SSL urls
2009-04-27T08:26:02.867297-05:00AndrewIngramhttps://djangosnippets.org/snippets/1467/<p>This snippet monkey-patches Django's reverse() method (use for generating URLs from vew functions and parameters) to allow certain areas of your site to automatically have URLs with the correct SSL domain in place.
</p>
<p>This saves you from having to use unnecessary redirects to guide users to an SSL-encrypted version of …</p>
Freely redistributableOverwrite some views in settings.py
2009-03-30T10:13:13.417447-05:00guettlihttps://djangosnippets.org/snippets/1404/<p>If you app defines some URLs with a name, and you want to overwrite this
at project level with a different view you can use this snippet. You only need
to change on line in the application code (the import statement).</p>
Freely redistributablelazy url reverse()
2007-12-07T15:07:25.951376-06:00guettlihttps://djangosnippets.org/snippets/499/<p>Since the decorators of your views are evaluated during parsing urls.py you have an 'chicken - egg' problem. The method reverse() can't be used since urls.py is not read.</p>
<p>This snippets evaluates reverse() lazy.</p>
<p><a href="http://code.djangoproject.com/ticket/5925">Related ticket: 5925</a></p>
<p>Django 1.4 (current trunk) has a lazy reverse.</p>
Freely redistributableUsing reverse() to do redirects
2007-08-24T22:57:57.718661-05:00ubernostrumhttps://djangosnippets.org/snippets/385/<p>When I initially set up my blog, I put together the archives with URL patterns like so:</p>
<ul>
<li><code>/weblog/2007/</code> goes to <code>archive_year</code></li>
<li><code>/weblog/2007/08/</code> goes to <code>archive_month</code></li>
<li><code>/weblog/2007/08/24/</code> goes to <code>archive_day</code></li>
<li><code>/weblog/2007/08/24/some-slug</code> goes to <code>object_detail</code></li>
</ul>
<p>The same patterns held for links, only the prefix was <code>/links/</code> instead of <code>/weblog/</code>.</p>
<p>For a …</p>
Freely redistributable