Login

Tag "url"

Snippet List

Create short URL redirects for site urls.

This allows you to host your own URL shortening service for your site's internal urls. By adding this class as a Mixin to your models, any model with a get_absolute_url method will have a get_short_url method also, which either returns an existing redirect or creates a new one and returns that. **Usage:** Import the class above, add the mixin to your model declaration, and ensure you have declared a get_absolute_url method. `class MyModel = (models.Model, ShortURL):` **Pre-requisites:** You must have the django.contrib.redirects app installed, and you must be using the RedirectFallbackMiddleware as a middleware class. **Settings:** Change the settings in the code above or set them in your settings.py file SHORTURL_CHARS: the characters to use when creating a shorturl SHORTURL_CHAR_NO = the number of characters to use in a shorturl SHORTURL_APPEND_SLASH = whether to append a slash to the end of the shorturl redirect **Notes:** The default settings will give you about 17 million different unique short URLs, reducing the number of characters used to 4 will give you 600,000 or so. That's enough that collisions will be quite rare for sites of a few thousand pages (collisions just result in a urls being generated until an unused combination is found) but if you've got a big site you'll probably want to explore a more robust solution with a proper hash function. [http://matt.geek.nz/blog/text/generating-short-urls-django-site-urls/](http://matt.geek.nz/blog/text/generating-short-urls-django-site-urls/)

  • url
  • redirect
  • tinyurl
  • short
Read More

Require Login Middleware

Wraps specified URL patterns with login_required decorator. Allows you to quickly require login for an area of your site based only on a URL pattern. Similar to [zbyte64's snippet](http://www.djangosnippets.org/snippets/966/)

  • middleware
  • authentication
  • url
  • login
  • auth
Read More

New Pattern

Now you can use object-oriented URL resolve and include and you can add function cp_before wich will be running before any any cp__* functions in his module

  • url
  • resolving
  • object-oriented
  • newpattern
  • pattern
Read More

Url persistance of GET variables

I'm using Django 0.96 for a project, and the url tag does not have all the capabilities I need. I want a way to persist all or some of the GET parameters between requests. I also want a way to add to the current url a list of extra parameters, or nullify certain parameters. In this snippet I am defining 2 tags ... link_persist and link_add. It only works with Django 0.96, as the API changed ... but maybe a good soul can fix it for 1.0, as I haven't had any time available. A tip for usage: if you specify a parameter as being the empty string, or None, the parameter will be removed from the link. When you specify a parameter already available in GET, it will replace it.

  • get
  • url
  • link
  • persistance
Read More

Add URL Segments to Templates

Add this code to you your context_processors.py in your project and then install it in your settings.py TEMPLATE_CONTEXT_PROCESSORS. In your template you can print out a segment of a url by using {{ segment_1 }}. For example if you're on the page "/mysite/section1/section2/" and you used {{ segment_2 }} it would print section1. This idea was taken from Expression Engines URL Segments, http://expressionengine.com/docs/templates/globals/url_segments.html. This comes in handy if you only want to do something in your template if the page your on has a particular segment. FYI, I haven't used this in a production setting yet so it could be buggy still.

  • template
  • url
  • path
  • segment
Read More

Require login by url

An example of using it in your settings.py: MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'util.loginmiddleware.RequireLoginMiddleware', ) LOGIN_REQUIRED_URLS = ( r'/payment/(.*)$', r'/accounts/home/(.*)$', r'/accounts/edit-account/(.*)$', ) In a nutshell this requires the user to login for any url that matches against whats listing in LOGIN_REQUIRED_URLS. The system will redirect to [LOGIN_URL](http://www.djangoproject.com/documentation/settings/#login-url)

  • middleware
  • authentication
  • url
  • login
  • auth
Read More

Add multiple parameters to the current url

Add multiple parameters to the current url.<br /> **Usage:**<br /> `{% addparam name1 value1 name2 value2 %}`<br /> or<br /> `{% addparam "name1" value1 "name2" value2 %}`<br /> variable can be use inplace of names and values<br /> example: <br />`{% addparam "view" message.id %}`

  • url
  • templatetags
  • parameter
Read More

Modify query string on a url

Modify a query string on a url. The comments in the code should explain sufficiently. String_to_dict, and string_to_list are also useful for templatetags that require variable arguments.

  • url
  • query
Read More

newforms-admin edit callback-url hook

NOTE: this is for **newforms-admin** I need edit links for items on my site, outside of the django admin -- however, I'm lazy, and don't want to build my own edit forms when there's a perfectly nice admin already there. Trick is, I need to be able to click a link to the django admin edit page for an item, and have it return to the calling page after saving. This chunk of code does the trick (the "real" version extra cruft, naturally) -- the links will bring up the django admin editor, then return to the calling page after saving.

  • newforms
  • admin
  • url
  • newforms-admin
  • newformsadmin
  • edit
  • callback
  • hook
Read More

A action decorator for URLs

This decorator handle a extra "action" parameter from an url and call this desired action in the provided views module. Example: from posts import views urlpatterns = patterns('posts.views', ... url(r'^(?P<id>\d+)/(?P<action>delete|publish|edit)/$', action(views), name="posts-action"), ... ) In templates: {% url posts-action id=post.id,action="delete" %}

  • rest
  • url
  • decorator
  • action
  • crud
Read More

Smart append slash middleware

This middleware replaces the behavior of the APPEND_SLASH setting in the CommonMiddleware. Please set `APPEND_SLASH = False` and `SMART_APPEND_SLASH = True` if you are going to use this middleware. In your URL patterns, omit the trailing slash for URLs you want accessible without the slash. Include the slash for those URLs you wish to be automatically redirected from an URL with the slash missing. If a URL pattern exists both with and without a slash, they are treated as two distinct URLs and no redirection is done. Example urlpatterns = patterns('some_site.some_app.views', (r'^test/no_append$','test_no_append'), (r'^test/append/$','test_append'), (r'^test/distinct_url$', 'view_one'), (r'^test/distinct_url/$', 'view_two'), ) Behavior of URLs against the above patterns with SMART_APPEND_SLASH enabled: http://some_site/test/no_append → test_no_append() http://some_site/test/no_append/ → 404 http://some_site/test/append → http://some_site/test/append/ http://some_site/test/append/ → test_append() http://some_site/test/distinct_url → view_one() http://some_site/test/distinct_url/ → view_two() This module is also available [in our SVN repository](http://trac.ambitone.com/ambidjangolib/browser/trunk/middleware/common.py).

  • middleware
  • url
  • slash
Read More

URL redirects middleware

Sometimes you need to make redirects that involve domains, you can't define those on the site urls, this middleware lets you define multiple redirects on your site settings. Note: *You also can use the web server to do this, but I have found this middleware a useful tool to quickly or temporarily define redirects*. Depending on your needs you may also find useful [snippet 434](http://www.djangosnippets.org/snippets/434/).

  • middleware
  • url
  • redirect
  • domain
Read More

ImageURLField for forms

A URL field specifically for images, which can validate details about the filesize, dimensions and format of an image at a given URL, without having to read the entire image into memory. Requires [Python Imaging Library](http://www.pythonware.com/library/pil/). *4th October, 2008* - updated for 1.0 compatibility.

  • image
  • pil
  • validation
  • url
  • form
  • field
Read More

71 snippets posted so far.