1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
malisit
  • 1,253
  • 2
  • 17
  • 36
  • Look for *context processor* how to include template data for every template: http://www.djangobook.com/en/2.0/chapter09.html – Mikko Ohtamaa Jul 11 '14 at 16:21

1 Answers1

2

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()?

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64