我从django开始,但这给我一个错误,我无法运行/blog/templates/index.html
PS: 我试过
url(r'^$', 'FirstBlog.blog.views.home', name='home')
或
url(r'^$', include('views.home'))
但这不起作用
我也试过
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
,但....
你帮我吗?完整项目:http://www.megafileupload.com/Uih/FirstBlog.tar.gz
django版本:1.7.7
答案 0 :(得分:0)
首先确保在settings.py中设置TEMPLATES_DIRS,它必须是绝对路径。
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )
接下来,如果您的模板文件夹在您的应用程序中,您只需要字符串'index.html',
# Function Based View
from django.shortcuts import render
def home(request):
return render(request, 'index.html') # Render syntax requires (request, template)
# Class based view
from django.views import generic
class Home(generic.TemplateView):
template_name = 'index.html'
对于基于类的视图网址:
from blog.views import Home
urlpatterns = [
url(r'^$', Home.as_view(), name='home'),
]
对于基于函数的视图网址:
url(r'^$', 'blog.views.home', name='home')