Code

Opened 6 years ago

Closed 6 years ago

#9593 closed (invalid)

permalink breaks when using include() in urls.py

Reported by: IanLewis Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

If you use include() to include urls in another urls.py then include will fail to generate the proper urls for urls in the included urls.py if the urls fall in a sub-path.

In the given urls.py

...
urlpatterns = patterns('',
    (r'', include('core.urls')),
    (r'^blog/(.*)', include('blog.urls')),
)
...

and models.py

...
class Post(models.Model):
    """Post model."""
    title           = models.CharField(_('title'), max_length=200)
    slug            = models.SlugField(_('slug'), unique_for_date='publish')
    author          = models.ForeignKey(User, blank=True, null=True)
    body            = models.TextField(_('body'))
    publish         = models.DateTimeField(_('publish'))
    
    def __unicode__(self):
        return u'%s' % self.title

    @permalink
    def get_absolute_url(self):
        # return '/blog/%s/%s/%s/%s' % (self.publish.year,self.publish.strftime('%b').lower(),self.publish.day,self.slug)
        return ('blog_detail', None, {
            'year': self.publish.year,
            'month': self.publish.strftime('%b').lower(),
            'day': self.publish.day,
            'slug': self.slug
        })
...

using the @permalink decorator will work for core.urls but it would not work for blog.urls. The permalink decorator simply returns an empty string.

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The only bug here is in the URL pattern you've created. You're capturing everything from blogs/ to the end of the string at the top level, so nothing will be seen by the second-level URLConf. Leave off the (.*) part and you'll find things work a lot better.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.