Pergunta

I have a model (class Explore) that has a GenericForeignKey relation with other classes. In that I have a method (get_renedered_html) to render the objects in a template (explore_photo.html) so that I can call the explore objects directly in the main template (index.html). But I can't get the request/current user object in the explore_photo.html only the object's user. However I can get the current/request user object in the main template (index.html).

I tried this in the explore_photo.html:

<p>{{ object.user }}</p><p>{{ user }}</p>
<p>{{ object.user }}</p><p>{{ request.user }}</p>

It only gives me the object's user and not the current/request user.

To check in the main template i.e. index.html I tried this:

<body>
    <p>Current user: {{ user }}</p>
    <h1>Explore</h1>
</body>

And it does, provide the current user object in the main template. What may be the cause? Or is it that I cant provide current user object to the `explore_photo.html' template? Please help me solve this problem. I would really appreciate it. Thank you!

This is my Explore model:

class Explore(models.Model):
    user = models.ForeignKey(User)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    pub_date = models.DateTimeField()
    content_object = generic.GenericForeignKey('content_type','object_id')

    def get_rendered_html(self):
        template_name = 'explore_photo.html'
        return mark_safe(render_to_string(template_name, {'object': self.content_object}))

explore_photo.html:

{% if object.display == 'P' or user == object.user %}
<div class="explore_photo">
    <img src="media/{{ object.image }}">
    <p class="photo_date">{{ object.pub_date|date:"F jS Y, P" }}</p>
    <p class="photo_description">{{object.description}}</p>
    <p class="photo_user">{{ object.user }}</p>
</div>
{% endif %}

index.html:

<body>
    <p>Current user: {{ user }}</p>
    <h1>Explore</h1>
    <div id="explore">
        {% for photo in photos %}
            {{ photo.get_rendered_html }}
            <hr>
        {% endfor %}
    </div>
</body>

Update:

def explore(request):
    photos = Explore.objects.all()

    return render(request, 'index.html', {'photos':photos})
Foi útil?

Solução

The problem is that you are not using a RequestContext in the sub-template - and you won't be able to, because you would need to pass the request into the get_rendered_html method, but you can't pass parameters into methods from the template.

You should rewrite this as a custom inclusion tag which can automatically take the context and render the template:

@register.inclusion_tag('explore_photo.html', takes_context=True)
def explore(context, obj):
    return {'user': context['user'], 'object': obj.content_object}

and call it in the template:

{% load my_template_tags %}
...
    {% for photo in photos %}
        {% explore photo %}
        <hr>
    {% endfor %}

Outras dicas

You need to add 'django.core.context_processors.request', in your settings.py

example

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top