Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使Django使用当前应用程序';s模板文件夹?_Python_Django_Django Templates - Fatal编程技术网

Python 如何使Django使用当前应用程序';s模板文件夹?

Python 如何使Django使用当前应用程序';s模板文件夹?,python,django,django-templates,Python,Django,Django Templates,现在我有一个Django项目,有三个应用程序。在我的上一个应用程序中,my views.py加载的index.html是另一个应用程序的templates文件夹中的index.html。请注意,这是它实际加载它的方式,但不是我希望它加载的方式。未使用定义了相应views.py的模板中的index.html。我想知道的是如何定义我的设置,以便使用当前应用程序目录的templates文件夹。这是关于模板的my settings.py: TEMPLATES = [ {

现在我有一个Django项目,有三个应用程序。在我的上一个应用程序中,my views.py加载的index.html是另一个应用程序的templates文件夹中的index.html。请注意,这是它实际加载它的方式,但不是我希望它加载的方式。未使用定义了相应views.py的模板中的index.html。我想知道的是如何定义我的设置,以便使用当前应用程序目录的templates文件夹。这是关于模板的my settings.py:

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',
            ],
        },
    },  
]

这是对index.html的调用

def indexView(request):
    form = FriendForm()
    friends = Friend.objects.all()
    return render(request, "index1.html", {"form": form, "friends": friends})


一个好的做法是,从应用程序的url.py内部添加:

app_name = 'your_app_name' # Now, you can call this app from the {% url tag %}
您的模板结构应包含应用程序目录,并且当您希望渲染到其他模板时,可以使用:

return render(request, 'myApp1/index.html')
return render(request, 'myApp2/index.html')
但请记住,在主模板文件夹中,您需要添加2个目录

templates/myApp1/index.html, 

最后,您是否将app_名称添加到您的应用程序URL?然后尝试第一个,或者完整的路径

{% url 'myApp1:post_friend' %} or full path {% url 'myApp1/index.html' %}

您的模板文件是否在同一目录中?如果它解决了您的问题,您可以将其标记为答案。谢谢执行此操作时,我在index.html中的ajax调用中得到url的反向查找错误。该url来自{%url'post_friend'%},其中post_friend是应用程序的url.py(而不是项目的url.py)中定义的url的名称。是否将应用程序名称添加到应用程序url中?在尝试了{%myApp1:post_friend%}之后,我不得不问,您是否在根URL模式中包含了应用程序URL?
{% url 'myApp1:post_friend' %} or full path {% url 'myApp1/index.html' %}