我是Django的新手,正在尝试配置url.py和views.py文档。这可能是一个非常简单的问题,但我无法设置url.py和views.py文档,以便localhost/index指向我创建的index.html文件。我已经遵循了Django项目教程的文字,并尝试了许多,许多变化,但这只是没有为我点击。任何帮助都将不胜感激!
index.html文件位于mysite/templates/index.html
我的文件夹结构是这样的。。。
mysite/
mysite/
__init__.py
settings.py
urls.py
wsgi.py
app/
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
templates/
css
img
js
index.html
My views.py包含:
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import Context, loader
from django.http import Http404
def index(request):
return render(request, "templates/index.html")
更新:我的文件夹结构现在如下所示:
mysite/
mysite/
__init__.py
settings.py
urls.py
wsgi.py
templates/
index.html
app/
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
static/
css
img
js
除了在
settings.py
中设置TEMPLATE_DIRS
之外:mysite/url.py
app/url.py
在
views.py
代码中,按原样将templates/index.html
更改为index.html
,模板应位于:另一方面,您的
css
、js
和img
文件夹最好放在其他地方,比如mysite/static
文件夹。是否在
TEMPLATE_DIRS
中定义了模板路径。设置.py
然后将模板文件夹移到
mysite > mysite > templates
然后不要这样做。这应该管用。
您的目录结构应该是:
相关问题 更多 >
编程相关推荐