2017-09-25 9 views
0

私はネストされたフォルダにDjangoのテンプレートに問題があります。 私のプロジェクトは、次のスキーマました:ジャンゴ1.11.5テンプレート

. 
    ├── 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は私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', 
      ], 
     }, 
    }, 
] 

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}) 

持っているそして、私のページのエラーは次である:

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) 

私はDjangoは異なるアーカイブのhtmlでテンプレートフォルダにサブディレクトリを読んでいることをどのように行うことができますか?

答えて

0

あなたのビューがevents/index.htmlをレンダリングする必要があります。

編集実際の問題は、テンプレートの設定です。 DIRS値は絶対パスのリストであるべきです。完全パスを与えるには、BASE_DIRの値を前に付ける必要があります。

'DIRS': [os.path.join(BASE_DIR, 'templates')] 

このリストには「イベント」を含めないでください。私が最初に言ったように、 "events/index.html"をレンダリングします。さもなければどのテンプレートがどのディレクトリにあるかについて非常に混乱し始めます。あなたの応答のための

+0

おかげで、私はそれを行うが、今の誤差がある:events_app 「/ でNoReverseMatch '' されていない登録の名前空間 リクエスト方法:\t リクエストURLをGET:\tにhttp:// localhostを:8000/ Djangoのバージョン:\t 1.11.5 例外タイプ:\t NoReverseMatch 例外値:\t 「events_app」は登録完全に別の問題である、名前空間」 –

+0

さて、ここで前に多くの何回も回答されているものではありません。 –

+0

はい、私はテンプレートディレクトリで私のベースのディレクトリを呼び出すときに私のビューにput events/index.htmlが必要な理由を理解していません 'DIRS':['/ templates'、 '/ templates/events' ]、 –

関連する問題