views:

660

answers:

1

I've building a app right now that I'm trying to keep properly decoupled from the other apps in my Django project (feel free to lecture me on keeping Django apps decoupled, I'd be happy to learn more any/all the time).

My problem is this: The get_ absolute_url() method I've written is returning a relative path based on my view. I think it's wrong to have to add a special named view in the project urls.py just so I can have absolute urls in my app, and I can't figure out what I'm doing wrong. So if someone can help me out, I'll really appreciate it (and mention you when I release this sucker!)

I have a project-level urls.py that includes another urls.py based on the URL pattern like so (the names are verbose for this example only):

project-urls.py

urlpatterns = patterns('',
    ('^$', direct_to_template, {'template': 'base.html'}),
    (r'^app', include('project.app.urls')),
)

app-urls.py

urlpatterns = patterns('',
    url(r'(?P<slug>[-\w]+)?/?$', 'app.views.home', name='app_home'),
)

Now, in my Model, I have something like this:

class AppModel(models.Model):
    title               = models.CharField(_('title'), max_length=100)
    slug                = models.SlugField(_('slug'), unique=True)

    @permalink
    def get_absolute_url(self):
        return ('app_home', None, {'slug': self.slug})

When I call {{ AppInstance.get_ absolute_url }} in the template, I get something like this:

/slug-is-here

Which is obvs not absolute & makes sense based on my urls.py. What should I change to get a real absolute url while keeping this app clean & not couple it too deeply w/the project?

A: 

Welp,

It turns out that when I was seeing this:

/slug-is-here

I should have looked closer. What was really happening was:

/app-pathslug-is-here

I was missing a trailing slash on my app's regex in my project urls.py.

So yea. let that be a lesson to y'all.

Pete Karl II