索夫,
我刚在我的ubuntu盒子上安装了django 1.9。
我想部署一个轻量级django应用程序。
我做了这个:
cd ~
django-admin startproject dj10
cd ~/dj10/dj10/
mkdir templates
echo hello > templates/index.html如何配置~/dj10/dj10/,以便当我得到/index.html时,django将服务于/index.html ??
我尝试过编辑~/dj10/dj10/urls.py,所以看起来如下:
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index, name='index'),
]然后我添加了这个文件:
~/dj10/dj10/views.py看起来是这样的:
from django.shortcuts import render
def index(request):
context = {}
return render(request, 'index.html', context)接下来,我启动了服务器:
cd ~/dj10/
python manage.py runserver这是我想要发生的事。
/index.html~/dj10/dj10/urls.py将/index.html与views.index匹配index()中运行views.pyindex()在这里找到index.html:~/dj10/dj10/templates/index.htmlindex()呈现index.html相反,我看到的是:
Page not found (404)
Request Method: GET
Request URL: http://lh:8000/index.html
Using the URLconf defined in dj10.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='index']
The current URL, index.html, didn't match any of these.那么,我可能在urls.py或views.py中出现了语法错误?也许我应该为. ~/dj10/dj10/settings.py添加一些语法?
这里还有一个问题:
如何配置~/dj10/dj10/,以便当我得到/index.html时,django将服务于/index.html ??
发布于 2016-01-01 02:25:47
简单地使用Django服务index.html是非常容易的:
git clone https://github.com/heroku/python-getting-started dj101
cd dj101
vi hello/templates/index.html
pip install -r requirements.txt
git commit -am hello
heroku local
heroku create dj101
git push heroku master发布于 2015-12-23 21:24:33
我不明白你为什么要专门为网址"index.html“服务。您已经将Django配置为响应路径"/“的HTML文件,这是正确的做法。没有理由在路径中使用模板名,最好不要这样做。
如果您确实出于某种原因想要这样做,则需要在urls.py中指定确切的文件路径:
url(r'^index.html$', views.index, name='index'),但正如我所说,你不应该这样做。
https://stackoverflow.com/questions/34443417
复制相似问题