首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django在更改最初加载的应用程序时没有查看正确的模板文件夹

Django在更改最初加载的应用程序时没有查看正确的模板文件夹
EN

Stack Overflow用户
提问于 2020-06-26 09:14:33
回答 2查看 183关注 0票数 0

在Django项目中,我有两个应用程序,名为file-managementdocs。当我第一次创建文件管理应用程序时,我的基本urls.py如下所示:

代码语言:javascript
运行
复制
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('file_management.urls')),
    path('admin/', admin.site.urls),
    path('file_management/',include('file_management.urls')),
    path('docs/',include('docs.urls')),
]

file_management文件夹中,我有一个urls.py,它定义了查找模板的位置:

代码语言:javascript
运行
复制
from django.urls import include, path
from .views import index

urlpatterns = [
    path('', index, name="file_management"),
    path('<int:file_id>/', index),
]

docs文件夹中,urls.py看起来如下所示:

代码语言:javascript
运行
复制
from django.urls import include, path
from .views import index

urlpatterns = [
    path('', index, name="docs"),
    path('<docs_page>/', index),
]

就像这样,一切都很好,我可以使用链接{% url 'docs' %}{% url 'file_management' %},管理页面可以通过/admin访问。

现在,如果我更改基本urls.py,使其指向docs.urls而不是用于''file_management.urls,如下所示

代码语言:javascript
运行
复制
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('docs.urls')),
    path('admin/', admin.site.urls),
    path('file_management/',include('file_management.urls')),
    path('docs/',include('docs.urls')),
]

加载项目时,我在正确的docs页面上登陆。但是如果我点击一个链接,我就会得到一个TemplateDoesNotExist at /file_management/。Django似乎在错误的文件夹中寻找模板:

代码语言:javascript
运行
复制
django.template.loaders.app_directories.Loader: /home/fiedler/anaconda3/envs/ct_env/lib/python3.7/site-packages/django/contrib/admin/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/anaconda3/envs/ct_env/lib/python3.7/site-packages/django/contrib/auth/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/sharOnStoNe/plotary/standalone/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/sharOnStoNe/plotary/shared/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/sharOnStoNe/plotary/file_management/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/sharOnStoNe/plotary/docs/templates/docs/file_management.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/fiedler/anaconda3/envs/ct_env/lib/python3.7/site-packages/bootstrap4/templates/docs/file_management.html (Source does not exist)

我在这里的URL设置中做错了什么?

views.py of file_management看起来如下所示:

代码语言:javascript
运行
复制
from django.shortcuts import render
from .forms import StandaloneFileForm
from .models import StandaloneFile
from django.conf import settings
import os

# Create your views here.

def index(request,file_id=None):
    if request.method == "POST":
        for file in request.FILES.getlist('file'):
            request.FILES['file'] = file
            form = StandaloneFileForm(request.POST, request.FILES)
            if form.is_valid():
                _new = form.save(commit=False)
                _new.save()
    else:
        form = StandaloneFileForm()
    all_entries = StandaloneFile.objects.all()

    if file_id:
        print("Received file id {} to delete.".format(file_id))
        if StandaloneFile.objects.filter(id=file_id).exists():
            os.remove(os.path.join(settings.MEDIA_ROOT,StandaloneFile.objects.get(id=file_id).file.url))
            StandaloneFile.objects.filter(id=file_id).delete()
        else:
            print("File does not exist.")

    return render(request, 'file_management/file_management.html',{"form":form,"entries":all_entries})

views.py of docs是这样的:

代码语言:javascript
运行
复制
from django.shortcuts import render
# Create your views here.

def index(request,docs_page='docs'):
    return render(request, 'docs/'+docs_page+'.html',{"page":docs_page})
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-26 11:16:17

在你的基地urls.py你有

代码语言:javascript
运行
复制
path('', include('docs.urls')),

在您的文档应用程序中有urls

代码语言:javascript
运行
复制
path('', index, name="docs"),
path('<docs_page>/', index),

这个<docs_page>就像一个变量。/之后的任何内容都将保存在这个docs_page中,这意味着任何url,比如localhost:8000/anything,都将由您的docs应用程序url/view方法处理。因为然后docs_page='anything'以同样的方式由docs应用程序处理localhost:8000/file_management,因为docs_page='file_management'是您获得模板错误的原因,因为您的模板不存在于doc中。编辑:

  1. 首先在您的基本url.py中删除冗余的path('docs/',include('docs.urls')),包含,因为您已经在第一行中包含了url。
  2. 在您的应用程序url前加上了许可前缀。就像

代码语言:javascript
运行
复制
urlpatterns = [
        path('files', view_for_files, name="file_home"),
        path('file_management/<file_id>', view_for_specific_file, name='file_with_id')

而您的file_management视图将类似于

代码语言:javascript
运行
复制
def view_for_specific_file(request, file_id):
    # your file_id now contains whatever was passed 
    # from ..:8080/file_management/5
    # so file_id = 5 now do what you want to do with it
票数 1
EN

Stack Overflow用户

发布于 2020-06-26 11:13:25

对于模板问题

根据信息,我假设您没有在file_management模板中注册settings.py应用程序

代码语言:javascript
运行
复制
In your file_management view you render with the following:

'file_management/file_management.html'

This will work if your structure looks like this

Root
    file_management
        templates
            file_management
                file_management.html
                

Or

Root
    templates
        file_management
            file_management.html

在您的设置中,验证以下内容:

代码语言:javascript
运行
复制
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'docs', 'templates'),
            os.path.join(BASE_DIR, 'file_management', 'templates'),
            
            
...

这将注册将所有模板文件夹合并到一个逻辑目录中。https://docs.djangoproject.com/en/3.0/topics/templates/

确保也提供静态和媒体文件。

代码语言:javascript
运行
复制
urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

https://docs.djangoproject.com/en/3.0/howto/static-files/提取

我建议如下..。

代码语言:javascript
运行
复制
urlpatterns = [
    path('', include('docs.urls')),
    path('admin/', admin.site.urls),
    path('file_management/',include('file_management.urls')),
    # path('docs/',include('docs.urls')),
]
    
urlpatterns = [
    path('', index, name="docs"),
    path('<docs_page>/', index),
]

or i assume you want this

urlpatterns = [
    path('', index, name="docs"),
    path('docs/<docs_page>/', index),
]

可选的步骤..。使用Urls,请确保添加名称空间。

代码语言:javascript
运行
复制
path('', include('docs.urls', namespace='docs')),
path('file_management/', include('file_management.urls', namespace='file_management')),

app_name = 'docs'
urlpatterns = [ ... ]

app_name = 'file_management'
urlpatterns = [ ... ]

# Usage example ... return redirect('docs:index')  return redirect('file_management:index')

如果这个修好了请告诉我。如果没有你的settings.py,很难确定这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62591631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档