Login

Tag "reverse"

Snippet List

HTTPS redirections middleware with updated URL template tag

This middleware redirects HTTP requests to HTTPS for some specified URLs, in the same way as [85](http://djangosnippets.org/snippets/85/). It also changes the `url` template tag to use the `https` scheme for the same URLs. For example, if you have the following URL pattern: url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'https': True}) then the template: {% from future import url %} {% url 'django.contrib.auth.views.login' %} will render: https://host.example.com/accounts/login/ and any plain HTTP requests to /accounts/login get redirected to HTTPS. URL patterns not marked with `'https': True` remain unaffected. Notes: * The HttpRequest object must be present in the template context as `request`, so add `django.core.context_processors.request` to `TEMPLATE_CONTEXT_PROCESSORS` and make sure to use `RequestContext`. * This snippet overrides the existing `url` template tag. Remove the last line and register the new `url` function properly, as a separate tag, if this makes you unhappy. You'd then have to change your templates to use it. * It would be nicer to change the way reverse look-ups behave instead of changing only the `url` template tag, but the URL resolver, and the `reverse` function, know nothing about requests, so have no way to find the correct host name.

  • middleware
  • template
  • url
  • ssl
  • reverse
  • https
  • redirection
  • tls
Read More
Author: xlq
  • 0
  • 2

Replacing pattern groups by values

This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example: IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works. For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'. For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', args=() and kwargs={'rev':'abc', 'id':123}, returns '/docs/123/rev/abc/' as well. When both args and kwargs are given, raises a ValueError.

  • regex
  • reverse
Read More

Using reverse with success_url in class based generic views

If you did tried to use `reverse` function to set `success_url` in class based generic views and you have got an exception, this helper function may help. Put this snipped in some file, for example utils.py and import this function.

  • reverse
  • class-based-generic-view
  • success_url
Read More

Doing redirect without request

When you neeed to do redirect and request object is not available, you can do it with exception. Put exception handler somewhere request is available, for example to middleware or ModelAdmin. Raise exception, where request is not available.

  • http
  • request
  • redirect
  • reverse
  • httpresponse
Read More

django subdomain support for both resolve and reverse.

Add these two middleware to the top of MIDDLEWARE_CLASSES. Add BASE_DOMAIN to your setting file : BASE_DOMAIN = '.13.com'. 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'), ) then {% url site_index id=4 %}<br /> {% url site_index2 %} in your template or the reverse function will work out urls like this: http://www.13.com/4/ http://news.13.com/abc/def/

  • url
  • reverse
  • resolve
  • subdomain
Read More

Ensure submitted slugs do not conflict with existing resolvable URLs

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 *does* successfully resolve, throws a validation exception. This indicates that the username chosen would be overriden (or, if you wrote your urls.py file badly, would override) an existing URL already recognized by your application. A much longer explanation can be found at [Dynamic names as first-level URL path objects in Django](http://www.elfsternberg.com/2009/06/26/dynamic-names-as-first-level-url-path-objects-in-django/).

  • registration
  • slug
  • reverse
  • resolve
Read More

Decorator to modify reverse() to render SSL urls

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. This saves you from having to use unnecessary redirects to guide users to an SSL-encrypted version of a page. This should still be used alongside a redirect-based method (such as [this snippet](http://www.djangosnippets.org/snippets/85/)) to ensure that the user can't access an unencrypted version of the page Simply add the code to the files mentioned in the code. This obviously won't work anywhere that doesn't use reverse(), the admin app seems to be an example of this.

  • url
  • ssl
  • reverse
  • permalink
Read More

Overwrite some views in settings.py

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).

  • urls
  • settings
  • reverse
Read More

lazy url reverse()

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. This snippets evaluates reverse() lazy. [Related ticket: 5925](http://code.djangoproject.com/ticket/5925) Django 1.4 (current trunk) has a lazy reverse.

  • decorator
  • reverse
  • lazy
Read More

Using reverse() to do redirects

When I initially set up my blog, I put together the archives with URL patterns like so: * `/weblog/2007/` goes to `archive_year` * `/weblog/2007/08/` goes to `archive_month` * `/weblog/2007/08/24/` goes to `archive_day` * `/weblog/2007/08/24/some-slug` goes to `object_detail` The same patterns held for links, only the prefix was `/links/` instead of `/weblog/`. For a forthcoming redesign/rewrite, I'm switching to using abbreviated month names (e.g., "aug", "sep", "oct", etc.) in the URLs, which means I need to redirect from the old-style URLs to the new. This snippet is the solution I hit upon. Two things are notable here: 1. Each one of these views uses [reverse()](http://www.djangoproject.com/documentation/url_dispatch/#reverse), called with the appropriate arguments, to generate the URL to redirect to. This means URLs don't have to be hard-coded in. 2. Each view takes an argument -- `object_type` -- which is used to generate the view name to pass to `reverse`, meaning that only one set of redirect views had to be written to handle both entries and links. This is just one of many handy tricks `reverse` can do :)

  • urls
  • reverse
  • redirects
Read More

11 snippets posted so far.