我在嵌套文件夹中遇到Django模板的问题。 我的项目有下一个模式:
.
├── eventus
│ ├── eventus
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ ├── urls.cpython-36.pyc
│ │ │ └── wsgi.cpython-36.pyc
│ │ ├── db.sqlite3
│ │ ├── settings
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ ├── __init__.cpython-36.pyc
│ │ │ │ ├── base.cpython-36.pyc
│ │ │ │ └── local.cpython-36.pyc
│ │ │ ├── base.py
│ │ │ ├── local.py
│ │ │ ├── prod.py
│ │ │ └── staging.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── manage.py
│ └── myapps
│ ├── __init__.py
│ ├── __pycache__
│ │ └── __init__.cpython-36.pyc
│ ├── events
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-36.pyc
│ │ │ ├── admin.cpython-36.pyc
│ │ │ ├── models.cpython-36.pyc
│ │ │ ├── urls.cpython-36.pyc
│ │ │ └── views.cpython-36.pyc
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ │ ├── 0001_initial.py
│ │ │ ├── 0002_auto_20170924_2140.py
│ │ │ ├── __init__.py
│ │ │ └── __pycache__
│ │ │ ├── 0001_initial.cpython-36.pyc
│ │ │ ├── 0002_auto_20170924_2115.cpython-36.pyc
│ │ │ ├── 0002_auto_20170924_2140.cpython-36.pyc
│ │ │ └── __init__.cpython-36.pyc
│ │ ├── models.py
│ │ ├── templates
│ │ │ ├── base.html
│ │ │ └── events
│ │ │ ├── base_events.html
│ │ │ └── index.html
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ └── users
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── admin.cpython-36.pyc
│ │ └── models.cpython-36.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-36.pyc
│ │ └── __init__.cpython-36.pyc
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── requirements
├── base.txt
├── local.txt
├── prod.txt
└── staging.txt
我的templates/base.html
是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock title %}</title>
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
我的templates/events/base.html
是
{% extends "base.html" %}
和mi templates/events/index.html
是
{% extends "events/base_events.html" %}
{% block title %}Home{% endblock title %}
{% block content %}
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Eventus</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav pull-right">
{% if user.is_authenticated %}
<li><a href="{% url 'events_app:panel' %}">Hola {{ user.username|capfirst }}</a></li>
{% else %}
<li><a href="{% url 'events_app:panel' %}">Sign Up / Login</a></li>
{% endif %}
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
<div class="jumbotron">
<div class="container text-center">
<h1>EVENTUS</h1>
<p>Bienvenido a EVENTUS, aquí podrás encontrar eventos que se den en tu ciudad e inscribirte on-line con un solo click.</p>
<p>¡<a href="{% url 'users_app:login' %}" role="button">Registrate</a> y crea tu evento ahora!</p>
</div>
</div>
<div class="container">
<div class="page-header">
<h4><strong>Eventos disponibles</strong></h4>
</div>
<div class="row">
<div class="col-md-2">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-tag"></span></span>
<select class="form-control">
<option>Categoría</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-credit-card"></span></span>
<select class="form-control">
<option>Tipo pago</option>
</select>
</div>
</div>
<div class="col-md-8">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>
<input type="text" class="form-control" placeholder="Busca el evento de tu gusto">
</div>
</div>
</div>
</div>
<p></p>
<div class="container eventos">
<div class="row contenedor-eventos">
{% for event in events %}
<div class="col-sm-1 col-md-3 ">
<div class="thumbnail equal">
<img src="{{ event.imagen.url }}" alt="250x180">
<div class="caption">
<h4><a href="">{{ event.name }}</a></h4>
<small class="date">{{ event.start }}</small>
<div class="place">
<small class="place">{{ event.place}}</small> /
</div>
<span class="views">{{ event.views }}</span>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="container categorias">
<div class="page-header">
<h4><strong>Categorías relacionadas</strong></h4>
</div>
<div class="row">
<div class="col-sm-1 col-md-12">
{% for category in categories %}
<a class="btn btn-default btn-lg btn-fix">
<span class="glyphicon glyphicon-tag"></span> {{ category.name }}
</a>
{% endfor %}
</div>
</div>
</div>
<div class="container organizadores">
<div class="page-header">
<h4><strong>Organizadores destacados</strong></h4>
</div>
<div class="row">
<div class="col-sm-1 col-md-2">
<div class="thumbnail">
<img src="http://placehold.it/300x180" alt="250x180">
<div class="caption">
Nombre organizador
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
我的view.py
是
from django.shortcuts import render
from .models import Event, Category
# Create your views here.
def index(request):
events = Event.objects.all().order_by('-created')[:6]
categories = Category.objects.all()
return render(request, 'index.html', {'events': events, 'categories': categories})
我的url.py
是
from django.conf.urls import url, include
urlpatterns = [
url(r'^$', include('myapp.events.views.index'),
url(r'^admin/', admin.site.urls),
]
模板中的和我的base.py
我有这个配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/templates', '/templates/events'],
'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',
],
},
},
]
我的页面错误是下一个:
TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.11.5
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: /Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages/django/template/loader.py in get_template, line 25
Python Executable: /Users/dmuino/.virtualenvs/Pruebas/bin/python
Python Version: 3.6.1
Python Path:
['/Users/dmuino/.virtualenvs/Pruebas/CursoProDjango/eventus',
'/Users/dmuino/.virtualenvs/Pruebas/lib/python36.zip',
'/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6',
'/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/lib-dynload',
'/Users/dmuino/anaconda/lib/python3.6',
'/Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages']
Server time: Mon, 25 Sep 2017 11:33:20 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /templates/index.html (Source does not exist)
django.template.loaders.filesystem.Loader: /templates/events/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages/django/contrib/admin/templates/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/dmuino/.virtualenvs/Pruebas/lib/python3.6/site-packages/django/contrib/auth/templates/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/dmuino/.virtualenvs/Pruebas/CursoProDjango/eventus/myapps/events/templates/index.html (Source does not exist)
如何使用不同的档案html读取模板文件夹中的子目录?
答案 0 :(得分:0)
您的观点应该呈现events/index.html
。
修改实际上问题出在您的模板设置中。 DIRS
值应为绝对路径列表。您需要添加BASE_DIR的值以提供完整路径。
'DIRS': [os.path.join(BASE_DIR, 'templates')]
请注意,您不应该包含&#39;事件&#39;在那个名单中。正如我原先所说,渲染&#34; events / index.html&#34 ;;否则你会开始对哪个模板在哪个目录中感到困惑。