在Django项目中,我有两个应用程序,名为file-management和docs。当我第一次创建文件管理应用程序时,我的基本urls.py如下所示:
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,它定义了查找模板的位置:
from django.urls import include, path
from .views import index
urlpatterns = [
path('', index, name="file_management"),
path('<int:file_id>/', index),
]在docs文件夹中,urls.py看起来如下所示:
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,如下所示
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似乎在错误的文件夹中寻找模板:
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看起来如下所示:
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是这样的:
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})发布于 2020-06-26 11:16:17
在你的基地urls.py你有
path('', include('docs.urls')),在您的文档应用程序中有urls
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中。编辑:
url.py中删除冗余的path('docs/',include('docs.urls')),包含,因为您已经在第一行中包含了url。urlpatterns = [
path('files', view_for_files, name="file_home"),
path('file_management/<file_id>', view_for_specific_file, name='file_with_id')而您的file_management视图将类似于
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发布于 2020-06-26 11:13:25
对于模板问题
根据信息,我假设您没有在file_management模板中注册settings.py应用程序
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在您的设置中,验证以下内容:
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/
确保也提供静态和媒体文件。
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/提取
我建议如下..。
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,请确保添加名称空间。
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,很难确定这个问题。
https://stackoverflow.com/questions/62591631
复制相似问题