Application Prefix

4 views
Skip to first unread message

Vance Dubberly

unread,
Aug 23, 2006, 11:01:25 PM8/23/06
to django...@googlegroups.com
So I've got this insane requirement that says I need to isolate all my django apps under a prefix ... as such

http://www.sitename.com/prefix/

I'm not really the kind guy  to hardcode the prefix into all my templates and so on. So I came up with a solution that makes my stomach turn. That is:

In setttings.py I define:

PREFIX = '/prefix'

and then in all my views I add something like this

from django.conf import settings

def index(request):
  return render_to_response('auth/index.html', { 'app_root': settings.PREFIX })

Then in the template I do something like this

<a href="{{ app_root }/thing/to/do">Do Thing</a>


Somebody please tell me I've been hitting the crack pipe a bit to hard and that there is a more elegant solution to this silliness, either through Apache ( mod_rewrite? ) or Django.


Vance

--
Most People are not even aware of their need to conform. They live under the illusion that they follow their own ideas and inclinations, that they are individualists, that they have arrived a their opinions as the result of their own thinking – and that it just happens that their ideas are the same as those of the majority
-- Eric Fromm

Russell Keith-Magee

unread,
Aug 24, 2006, 12:40:47 AM8/24/06
to django...@googlegroups.com
On 8/24/06, Vance Dubberly <altje...@gmail.com > wrote:
So I've got this insane requirement that says I need to isolate all my django apps under a prefix ... as such
...

Somebody please tell me I've been hitting the crack pipe a bit to hard and that there is a more elegant solution to this silliness, either through Apache ( mod_rewrite? ) or Django.

That's a very nice crack pipe you have there :-)

You are correct in thinking that the better approach is to use apache. You shouldn't need to modify your django app to relocate it on your development server - depending on exactly what you want to achieve, a combination of Location definintions, rewrite rules and/or aliases should get the job done. Check out the Django modpython documentation (and the apache modpython docs themselves) for more details.

Yours,
Russ Magee %-)

Ivan Sagalaev

unread,
Aug 24, 2006, 2:47:06 AM8/24/06
to django...@googlegroups.com
Vance Dubberly wrote:
> I'm not really the kind guy to hardcode the prefix into all my
> templates and so on.

There is not yet officially introduced and documented but working
feature (The Right Feature!) for making URLs in your templates to
automatically point to your views by their current path. It's called
'reverse URL lookup'. It works by scanning actual regexps in your urlconf.

I've created a template tag that wraps it to be usable in templates:

class URLNode(template.Node):
def __init__(self, view_name, args):
self.view_name = view_name
self.args = args

def render(self, context):
from django.core.urlresolvers import reverse, NoReverseMatch
args = [arg.resolve(context) for arg in self.args]
project_name = settings.SETTINGS_MODULE.split('.')[0]
try:
return reverse(project_name + '.' + self.view_name, args=args)
except NoReverseMatch:
return ''

@register.tag
def url(parser, token):
bits = token.contents.split()
if len(bits) > 2:
args = [parser.compile_filter(arg) for arg in bits[2].split(',')]
else:
args = []
return URLNode(bits[1], args)

The usage is:

{% url app_name.views.artist artist.id %}

I.e. you provide a path to a view and actual parameters that it accepts
and it returns something like '/path/to/your/project/artist/15/'
('artist' and '15' are for example).

It's a bit limited since it works only with positional arguments in
views but not with keyword ones. This is not hard to do in code since
'reverse' function itself already support it. I just couldn't come up
with a nice syntax for a tag :-)

Adrian Holovaty

unread,
Aug 26, 2006, 12:30:01 PM8/26/06
to django...@googlegroups.com
On 8/24/06, Ivan Sagalaev <Man...@softwaremaniacs.org> wrote:
> I've created a template tag that wraps it to be usable in templates:
> [...]

> It's a bit limited since it works only with positional arguments in
> views but not with keyword ones. This is not hard to do in code since
> 'reverse' function itself already support it. I just couldn't come up
> with a nice syntax for a tag :-)

Nice, Ivan! I've been meaning to write something like this...Would you
be willing to contribute it to the framework?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Ivan Sagalaev

unread,
Aug 26, 2006, 1:30:19 PM8/26/06
to django...@googlegroups.com
Adrian Holovaty wrote:
> Nice, Ivan! I've been meaning to write something like this...Would you
> be willing to contribute it to the framework?

Sure, as usually :-) Have you are: http://code.djangoproject.com/ticket/2606

I also added a docstring that may be a bit clumsy and will certainly
require a proof-read.

Reply all
Reply to author
Forward
0 new messages