[英]TemplateDoesNotExist at / index.html
我的 Django 項目有一些問題。
我是用 django 制作網絡應用程序的新手。 請幫我。
所以,
這是我的目錄路徑,
-lullabyluna
----app_index
------------admin.py
------------index.html
------------__init__.py
------------migtations
------------models.py
------------tests.py
------------views.py
----index.html(I wonder this files path.)
----lullabyluna
------------__init__.py
------------settings.py
------------url.py
------------wsgi.py
----manage.py
----static
我的 settings.py 文件包括:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_index',
)
這是我的 url.py 文件
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('app_index.views',
# Examples:
url(r'^$', 'home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
最后這是我的 app_index/views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
def home(request):
return render_to_response('index.html',{},context_instance=RequestContext(request))
所以,我想知道正確的“index.html”文件的路徑在哪里。
在 settings.py 中添加這個
TEMPLATE_DIRS = (
PROJECT_PATH
)
(或)將您的 index.html 移動到模板文件夾並執行此操作,
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/',
)
在 pythonwhere.com 中,您需要在 settings.py 中執行此操作
'目錄':['/home/your_user_name/your_project/templates'],
在主項目文件夾的“settings.py”中提及您的應用程序名稱:
INSTALLED_APPS = [
'Your_AppName',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
在此處輸入代碼 lullabyluna ................. 將您的模板(index.html、home.html 等)放在這里,並在此處放置您的靜態文件夾,現在您將在 index 內部獲得索引和靜態文件{ % load static%} 和 html 的結尾 {% block content %} 和 {% endblock %} 現在你的頁面是動態的
我認為,問題可能出在“app_index/views.py”上。 您要做的是,在應用程序文件夾(app_index)中創建一個模板文件夾。 在模板文件夾中,創建另一個名為(app_index)的文件夾,然后將 index.html 文件放在這里。 在你這樣做之后,在你的 app_index/views.py 中,改變這個; " return render_to_response('index.html',{},context_instance=RequestContext(request)) "
to;
return render_to_response('app_index/index.html',{},context_instance=RequestContext(request))
希望它對你有用,如果所有其他東西都沒有問題。
from django.template import RequestContext
from django.shortcuts import render_to_response
def home(request):
return render_to_response('index.html',{},context_instance=RequestContext(request))
您缺少指向應用程序索引的路由。 而不是上面,它應該是
def home(request):
return render_to_response('**app_name/**index.html',{},context_instance=RequestContext(request))
我已經嘗試了很多使用 render_to_response 來做到這一點,但請注意 django 仍然不使用 render_to_response。 使用render而不是render_to_response
所以,
在文件夾 {The_name_of_the_Project} 中,去編輯settings.py
在TEMPLATES 中,用包含靜態 html 文件的文件夾的路徑填充DIRS 。 如果 .html 文件存在於您的 {The_name_of_the_Project} 文件夾中,則路徑類似於:“/home/your_username/The_name_of_the_Project/”。
因此,如果 {The_name_of_the_Project} 文件夾中包含靜態 .html 文件,DIRS 必須如下所示: DIRS =['/home/your_username/your_project_name/']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.