Search code examples
pythondjangotemplatesfor-loopdjango-context

Django display a view in every page


I want to run my for loop in every page. I have same of the code below in my views.py.

def hepsi(request):
    basliklar = Baslik.objects.filter(active=True).order_by('-updated')
    return render_to_response("base.html", locals(), context_instance=RequestContext(request))

and this is my urls.py part:

url(r'^$', 'hepsi', name = "hepsiliste"),

I have a for loop in base.html:

{% for baslik in basliklar %}
     <div>
         <a href="{% url "tek_baslik" baslik.slug %}"><h2> {{ baslik }} </h2></a>
         <p><i class="fa fa-user"></i> {{ baslik.user }}</p>
     </div>
{% endfor %}

it works in home page but it does not work in other pages like as /baslik/x

How can I make this work in every page user wants?


Solution

  • You should add a context processor

    https://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors

    so that each template will found baslikar variable in the context.

    However it is not clear how your hepsi function works, since the variable baslikar is never used... Maybe it is computed in locals()?