boyenec
boyenec

Reputation: 1617

Django how to pass context or model objects in base.html without pass views in url

I know I can pass context using somethings like this in base.html

class MyViews(request):
      #my code.....
      context= {#my context} 
      return render(request, 'base.html', context)

then pass the views in url. But is there any to pass context in base.html without using url?? any idea?? I also tried this

'context_processors': [
        ...
        # add a context processor
        'my_app.context_processor.my_views',

    ], #getting  this error No module named 'contact.context_processor'

Upvotes: 0

Views: 273

Answers (1)

Abduvahob Kaxarov
Abduvahob Kaxarov

Reputation: 247

     You can use context_pro.py it will send context objects to all html files but I dont recommend it. because context pro send contexes all html files if you want to use. first open context_pro.py file


from .models import *

def funcName(request):
    some = Some.objects.all()
    anther_one = Another.objects.all().order_by('-date')[:16]
    context = {

    'some':some,
    'anther_one':anther_one,
    }
    return context

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'yourApp.context_pro.funcName',
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Upvotes: 1

Related Questions