Wednesday, March 29, 2017

NoReverseMatch when trying to use get_absolute_url with custom template tag

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?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/aae357a0-84a7-46bd-a93d-4aac61f352c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment