On Sun, 2009-02-22 at 15:18 -0800, nixon66 wrote: > not sure I'm understanding how get_absolute_url to work. > > in my model I created this > > > class State(models.Model): > name = models.CharField(max_length=80) > slug = models.CharField(max_length=80) > > def __unicode__(self): > return self.name > > def get_absolute_url(self): > return "/state/%s/" % self.slug > > > and my template looks like this: > > {%block content %} > > <ul> > > {% for state in state_list %} > > <li><a href ="{{state.get_absolute_url }}">{{ state.name }}</a></li> > > {% endfor %} > > </ul> > > {% endblock %}
That all looks fine. It should work. > > seems like this would work. But I get an empty page with nothing > unless I drop the "state.name" and just go with state. That suggests state.name is the empty string, possibly. > Then I get an > empty string for the url. I'm not understanding the docs on how to do > this. Can anyone point me to a good example of how this should work. Hmm ... are you sure "state" is really a State object? Because if it was, say, a string, then state.get_absolute_url would return an empty string to the template (as the get_absolute_url attribute or method doesn't exist). You can test out what should be happening by opening up an interactive prompt (python manage.py shell) and testing it out manually. Something like this: >>> st = State.objects.get(pk=1) ... >>> st.name u"fred" >>> st.get_absolute_url() "/slug/foo/" You could also simplify your template or view and make it work with just a single state object first (instead of the list). Make sure that when you pass in a hard-coded State instance, it displays something. Then move onto dynamically working out which State instances to display. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---