In my template, I use a method in a model as follows...
The mode开发者_StackOverflowl is defined like this:
class Category(model.Model):
title = models.CharField('title', max_length=255)
description = models.TextField('description', blank=True)
def pub_set(self):
...
return somedata
The Template is used something like this:
{{ category.pub_set }}
I want to be able to get the current user in pub_set
I'm not 100% clear what you are asking. Inside of Category.pub_set() are you trying to access the request.user? If so, you can't do that unfortunately. There might be some sort of hack that will work, but it isn't the recommended way in Django.
You might want to check out template tags, they will allow you to pass your category and user from the template, and then do what you need to do, and return your result. Here is a link to the documentation.
http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#passing-template-variables-to-the-tag
Another approach is to do what you need to do in the view, and then save your result in the page context, where it will be available in the template.
If I had a little more insight into what you are trying to do, it might make it easier to steer you in the right direction.
精彩评论