将模板导入Django中的基本模板

时间:2014-05-06 16:54:33

标签: html django

我希望能够在base.html中显示gallery.html。我使用开发人员工具检查了源代码,当我在http://localhost:8000/gallery/

时,找不到gallery.html

注意:

我目前在我的图库应用中没有urls.py。我的项目目录中只有urls.py。

项目的urls.py:

from django.conf.urls import patterns, include, url
from home.views import loadHomepage
from gallery.views import loadGallery
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    url(r'', 'home.views.loadHomepage'),
    url(r'gallery/$', 'home.views.loadGallery'),
)

图库应用views.py:

from django.shortcuts import render_to_response

def loadGallery(request):
    return render_to_response('gallery.html')

项目结构

/proj
   /gallery
      /static
         /css
            gallery.css
      /templates
         gallery.html
   /proj
   /templates
      base.html
   /static
      /css
         base.css

base.html文件:

{% load staticfiles %}

  <body>
    <div class="container">
      <div class="home-button">
      </div>
      <div class="sidebar">
      </div>
      <div class="content-container">
        {% block content %}

        {% endblock %}
      </div>

    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
  </body>

gallery.html:

{% extends base.html %}

{% block content %}
<link rel="stylesheet" type="text/css" href="{% static "css/gallery-container.css" %}">
<div class="container">
hi there!
fgdfjkhlkjhjk
</div>

{% endblock %}

settings.py:

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_STORAGE = (
    'django.contrib.staticfiles.storage.StaticFilesStorage'
)

STATICFILES_DIRS = (
     os.path.join(BASE_DIR, 'static'),
)    

STATIC_ROOT = os.path.realpath(os.path.join(BASE_DIR, '..', 'PersonalWebsiteAssets'))

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'),
    os.path.join(BASE_DIR,'home/templates'),    
    os.path.join(BASE_DIR,'gallery/templates'),
    os.path.join(BASE_DIR,'contact/templates'),
    os.path.join(BASE_DIR,'about/templates'),
)

2 个答案:

答案 0 :(得分:1)

您需要在所有模板中{% load staticfiles %},无论您是否继承 base.html (假设您在模板中使用静态文件)。

您的 urls.py 也应该类似于:

from django.conf.urls import patterns
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^gallery/', TemplateView.as_view(template_name=gallery.html")),
)

答案 1 :(得分:1)

我认为你的loadGallery视图根本就没有被调用。对于loadHomepage,以前的URL在正则表达式中没有任何开始/结束锚点,因此它将捕获所有 URL。 Django的URL解析器只会将所有内容路由到那里,并且永远不会进入&#34;画廊&#34; URL。

你应该按如下方式进行:

url(r'^$', 'home.views.loadHomepage'),
url(r'^gallery/$', 'home.views.loadGallery'),

此时您将遇到图库模板中的错误,例如extends标记的参数需要在引号中。