On Mon, Jun 25, 2012 at 8:40 AM, Matthew Meyer
<matthewwilliamme...@gmail.com> wrote:
> Hi all, Django newbie here,
>
> I have a simple blog app I am working on and am trying to figure out the
> best way to create a permalink for tags that I have added for blog posts.
> Basically, my models.py consists of two classes: Post and Tag, which has
> Post as a foreign key: http://dpaste.org/Ojhh5/
>
> I am then trying to access the permalink to the tag using: {{
> tag.get_absolute_url }}

Hi Matthew,

If {{ tag.get_absolute_url }} isn't rendering anything, once of three
things must be happening:

 1) get_absolute_url isn't defined. Tag clearly has a get_absolute_url
method, and there's no typo there, so this isn't the problem.

 2) get_absolute_url is raising an error, or returning None. You can
check this by putting some debug in the get_absolute_url method, or
sticking a python debugger breakpoint in the method.

 3) tag isn't a valid variable in the template you're rendering.

In the context you're rendering, tag is a variable in a for loop -- so
the first think to establish is whether you're iterative over anything
at all. Are you seeing anything rendered by the for loop, or is it
just the {{ tag.get_absolute_url }} call that is returning an empty
string? If it's just get_absolute_url, then the problem is (2);
otherwise, you need to look into (3).

The other way to debug this -- fire up a Python shell. Django's
template language silences almost all errors. This is by design, since
the worst thing you could have in production is error messages leaking
through to the user. However, the upshot of this is that templates can
be harder to debug, since there's no difference between "doesn't work"
and "nothing to print". However, if you fire up a Python shell
(./manage.py shell), you can interrogate the same API that the
template is using:

# Do your imports, then...
# Get the post you want to render
>>> post = get_object_or_404(Post, tag='my tag')

# Print the list of tags
>>> print post.posttag_set.all()

# Print the absolute URL for the first tag
>>> print post.posttag_set.all()[0].get_absolute_url()

If there's an error being raised somewhere, these calls will show you
the full error trace, rather than silencing the errors.

Hope that helps you debug the problem!

Yours,
Russ Magee %-)

-- 
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.

Reply via email to