使用Django 1.8设置模板和静态文件

时间:2016-02-17 18:41:50

标签: python django

我在模板上工作时遇到了一些麻烦。总体上非常不确定如何在项目目录中设置静态文件和模板。

我是否应该在开发时将我的模板放在app文件夹中以及app文件夹中的静态文件?

这是输出:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Users/username/Dev/test_dev_01/projects/test/main/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/tinymce/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mce_filebrowser/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/django/contrib/auth/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/django/contrib/sitemaps/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/conf/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/core/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/generic/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/pages/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/blog/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/forms/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/galleries/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/mezzanine/accounts/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/avatar/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/filebrowser_safe/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/grappelli_safe/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/django/contrib/admin/templates/main/templates/index.html (File does not exist)
/Users/username/Dev/test_dev_01/lib/python3.4/site-packages/django_comments/templates/main/templates/index.html (File does not exist)

urls.py:

url(r"^$", "main.views.home", name="home"),

views.py:

def home(request):
    return render(request, "main/templates/index.html", context)

settings.py:

PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
PROJECT_ROOT = BASE_DIR = os.path.dirname(PROJECT_APP_PATH)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            #os.path.join(PROJECT_ROOT, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                "django.core.context_processors.debug",
                "django.core.context_processors.i18n",
                "django.core.context_processors.static",
                "django.core.context_processors.media",
                "django.core.context_processors.request",
                "django.core.context_processors.tz",
                "mezzanine.conf.context_processors.settings",
                "mezzanine.pages.context_processors.page",
                "main.context_processors.global_settings",
            ],
        },
    },
]

这可能是什么原因?我无法弄明白。

1 个答案:

答案 0 :(得分:2)

我只是因为您没有提供此信息而推断出这一点:

  1. 您的应用名为if(payload['username'] == null) { //do something if it's null } else { //it's not null }
  2. 您的模板main 真的位于: index.html
  3. 如果我的推测是正确的,您对渲染的调用应该是:

    /Users/username/Dev/test_dev_01/projects/test/main/templates/index.html
相关问题