简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM