NoReverseMatch when trying to use get_absolute_url with custom template tag

36 views
Skip to first unread message

Will Holmes

unread,
Mar 29, 2017, 4:19:08 PM3/29/17
to Django users

I am making a calendar app which has a custom template tag that takes the python HTML template function and overlays objects from the Events model on the correct days. I am trying to get the displayed objects to have a link directly to an object detail/edit view and am trying to use get_absolute_url and to reverse render this view. This is necessary as the custom template tag doesn't load correctly if I try to hardcode {% url %} template tags into it for each event via a for loop. I have spent some hours looking through stack overflow questions with no luck and have even changed my reverse to the object ID rather than the title of the event. I am hoping this is just a small thing that I have overlooked but no sure.


view:

def home(request, month=None, year=None):
    if month == None:
        _date = datetime.now()
    else:
        _date = date(int(year), int(month), 1)
    title = "%s, %s" % (_date.strftime("%B"), _date.strftime("%Y"))

    return render(request, 'calendar.html', calendar(_date, title))


url:

app_name = 'cal'
urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^newevent/$', views.newEvent, name='newevent'),
    url(r'^(?P<id>\d+)$/', views.viewEvent, name='viewevent'),
    url(r'^(?P<month>\d+)/(?P<year>\d+)$', views.home, name='another-month')
]


HTML:

    <div>
      {% load calendarify %}
      <span id="calendarify">{% calendarify year month event_list %}</span>
    </div>


Template tag relevant function:

    def formatday(self, day, weekday):
        if day != 0:
            cssid = self.cssclasses[weekday]
            cssclass = "daybox"
            if date.today() == date(self.year, self.month, day):
                cssid += ' today'
            if day in self.events:
                cssid += ' filled'
                body = ['<ul>']
                for event in self.events[day]:
                    body.append('<li>')
                    body.append('<a href="%s">' % event.get_absolute_url())
                    body.append(esc(event.title))
                    body.append('</a></li>')
                body.append('</ul>')
                return self.day_cell(
                    cssclass, cssid, '<span class="dayNumber">%d</span> %s' % (
                        day, ''.join(body)))
            return self.day_cell(
                cssclass, cssid, '<span class="dayNumberNoReadings">%d</span>' % (day))
        return self.day_cell('nodaybox', 'noday', '&nbsp;')


Model:

class Events(models.Model):
    ...

    def get_absolute_url(self):
        return reverse('cal:viewEvent', args=[str(self.id)], current_app='cal')


Sorry about the load of code posted. Any chance anyone knows what might have caused this issue?

Melvyn Sopacua

unread,
Mar 29, 2017, 5:02:28 PM3/29/17
to django...@googlegroups.com

On Wednesday 29 March 2017 09:16:17 Will Holmes wrote:

 

> url(r'^(?P<id>\d+)$/', views.viewEvent, name='viewevent'),

 

name = viewevent

 

> def get_absolute_url(self):

> return reverse('cal:viewEvent', args=[str(self.id)],

> current_app='cal')

 

name = viewEvent

 

> Sorry about the load of code posted. Any chance anyone knows what

> might have caused this issue?

 

Do you see it now?

--

Melvyn Sopacua

Will Holmes

unread,
Mar 30, 2017, 12:42:41 PM3/30/17
to Django users
Oh yeah i see.

Brilliant sorted it! Thanks for your help Melvyn.
Reply all
Reply to author
Forward
0 new messages