Login

Tag "url"

Snippet List

@url decorator - getting rid of urlpatterns

The rationale behind this decorator is described in django-users google group. Usage: === urls.py === urlpatterns = patterns('', (r'^', include('apps.app1.views')), (r'^app2', include('apps.app2.views')), ) === apps/app1/views/__init__.py === @url(r'^index/$') def index(request): ... @url(r'^news/$') def news(request): ... urlpatterns += include_urlpatterns(r'^members', 'apps.app1.views.members') === apps/app1/views/members.py === @url(r'^profile/$) def profile(request): .... @url(r'^secure/$) def secure(request): ... @url(r'^path1/$', '^path2/$') # you can specify several patterns def multipath_view(request): ... def helper(): # easily distinguishable - no @url! ... Summarizing, the benefits are: * no more creating and supporting urlpattern maps (less files, less code, more DRY) * have the url associated with a view in-place * easily see if a function is a view * fully compatible with other chained decorators

  • view
  • url
  • decorator
  • urlpatterns
Read More

paginator using url tag

Example for provided django-tagging url snippet: {% paginator 4 image_tag_paged tag=tag page %} links then equals {% url image_tag_paged tag=tag,page=n %}

  • tag
  • pagination
  • url
  • paginator
Read More

I18n URLs via Middleware

This is an example middleware that is highly inspired by how Symfony handles [i18n in URLs](http://www.symfony-project.com/book/trunk/13-I18n-and-L10n#Changing the Culture for a User). You basically set a (?P<dj_culture>[\w-]+) pattern in your URL and this middleware will determine the language to use for the i18n toolkit for Django. It also removes the dj_culture parameter after dealing with it, so that you don't have to change all the views you want this middleware to work with.

  • middleware
  • i18n
  • url
Read More

Hierarchical page slugs

This allows for urls in the form of `/grandparent-slug/parent-slug/self-slug/` where the number of parent slugs could be 0 to many. You'll need to make sure that it is your last urlpattern because it is basically a catch-all that would supersede any other urlpatterns. Assumes your page model has these two fields: * `slug = models.SlugField(prepopulate_from=("title",), unique=True)` * `parent = models.ForeignKey("self", blank=True, null=True)`

  • slug
  • url
Read More

Add parameters to the current url

Often a page contains a link to itself, with an additional parameter for in the url. E.g. to sort the page in a different way, to export the data into another format, etc... This tag makes this easy, avoiding any hardcoded urls in your pages.

  • url
  • parameter
Read More

Never cache a group of URLs

This is a special URL patterns replacement that prevents caching of any URL listed within it. We needed this in Review Board to prevent the JSON API function results from being cached in Internet Explorer.

  • urls
  • cache
  • url
  • patterns
Read More

media_url context variable

with this you can have context variables which know the media url and the urls of all your applications, if you need it. save the code as myapp/context_processors.py and add the following line to `TEMPLATE_CONTEXT_PROCESSORS` setting "mysite.myapp.context_processors.url_info", For each application you need to know the url set `MYAPP_URL` and add it to dict.

  • url
  • media
  • context
Read More

Digg Style URL String Parser

Does a digg url effect to a string, can be useful for using an item's title in the url, from this: .hi's., is (a) $ [test], will it "work"/ \ to this: his_is_a_test_will_it_work I understand this isn't a very well made script, I am not very good at string manipulation. But I would be happy if someone would recode it in a faster, more managable way. I recomend saving the rendering.

  • url
  • clean
  • simatic
Read More

Url filter middleware

How to config it ------------------ You can treat it as a micro url filter framework. Before you using it, you should setup some options about it. The option entry shoud be like this: FILTERS = ( (r'^user/(?P<user_id>\d+)/', 'apps.users.filter.check_valid_user'), ) FILTERS should be a list or tuple with two elements tuple item. The format should be like: (url_patterns, function) And url_patterns could be a single regex expression or a list/tuple regex expressions, So you can set multi regex expression in it. And the regulation is just like url dispatch, as above example, the url pattern is: r'^user/(?P<user_id>\d+)/' So you can see, you can set parameter name `user_id`, then it'll be passed to the function behind. Function can be a string format, just like above example, and it can be also a real function object. It'll only impact request. How to write filter function ------------------------------- According above example, I define a url pattern, and what to check if the user is a valid user, and if the user is visiting his own urls, so the filter function could be: from django.contrib.auth.models import User from utils.common import render_template def check_valid_user(request, user_id): if request.user.is_anonymous(): return render_template(request, 'users/user_login.html', {'next':'%s' % request.path}) try: person = User.objects.get(pk=int(user_id)) except User.DoesNotExist: return render_template(request, 'error.html', {'message':_("User ID (%s) is not existed!") % user_id}) if person.id != request.user.id: return render_template(request, 'error.html', {'message':_('You have no right to view the page!')}) I think the code is very clear. And you can use it filtermiddleware to do like user authentication check, and other checking for url. BTW, render_template is comes from [Snippets #4](http://www.djangosnippets.org/snippets/4/)

  • middleware
  • filter
  • url
Read More

71 snippets posted so far.