Django view render for a template from another application

I'm trying to display a template from another application, but not sure how to import, or do I need to import using templates?

Structure

project->
    main app->
       templates->
         pages->
           home.html
         account ->
    current app->
       views.py

my views.py file is in the current application and is trying to access templates in the main application (in subfolders of pages).

How do I do this:

def temp_view(request):
    ....
    return render(request, "home.html",context)
+4
source share
2 answers

First, you should most likely configure the static templates path in settings.py with something similar to this

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

APP_DIRS=Truemeans that Django will look for patterns in each application directory in your case main_app/and current_app/:

, , :

def temp_view(request):
   ....
   return render(request, "pages/home.html",context)

Django main_app/ , pages/home.html

+3

, ?

from mainapp.models import mainappmodel
def currentappview(request):
    all = mainappmodel.objects.all()
    return render(request, "pages/home.html", {'all': all})
-1

Source: https://habr.com/ru/post/1620158/


All Articles