I didn't think to check the shell! It is definitely issue 2.

I was able to output: 

>>> for tag in post.posttag_set.all(): tag.get_absolute_url()
... 
'/blog/footag/'

which is exactly what I expect. (I have one post with the tag "footag")

post = get_object_or_404(Post, tag='footag') returns the error:
FieldError: Cannot resolve keyword 'tag' into field. Choices are: html, id, 
posttag, pub_date, slug, title
 
So I tried: 
post = get_object_or_404(Post, posttag='footag') which returned:
ValueError: invalid literal for int() with base 10: 'zzz'

I think a lot of my issues are being caused by my ignorance of how I would 
refer to a Post objects tag since they are only related via foreign key.

btw, my templates print the tags perfectly with {% for tag in 
post.posttag_set.all %}
so seeing >>> for tag in post.posttag_set.all(): tag.get_absolute_url() 
print the tag url exactly came as a surprise to me.

On Sunday, June 24, 2012 9:13:32 PM UTC-4, Russell Keith-Magee wrote:
>
> On Mon, Jun 25, 2012 at 8:40 AM, Matthew Meyer 
> <matthewwilliamme...@gmail.com<https://mail.google.com/mail/?view=cm&fs=1&tf=1&source=mailto&to=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 view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/kezRIPNyCAsJ.
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