Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何简单地使用django提供index.html?_Python_Html_Django - Fatal编程技术网

Python 如何简单地使用django提供index.html?

Python 如何简单地使用django提供index.html?,python,html,django,Python,Html,Django,sof 我刚刚在我的ubuntu盒上安装了django 1.9 我想部署一个轻量级的django应用程序 我这样做: cd ~ django-admin startproject dj10 cd ~/dj10/dj10/ mkdir templates echo hello > templates/index.html 如何配置 ~/dj10/dj10/ 这样django将提供~/dj10/dj10/templates/index.html 当我得到/index.html ? 我试着编辑

sof

我刚刚在我的ubuntu盒上安装了django 1.9

我想部署一个轻量级的django应用程序

我这样做:

cd ~
django-admin startproject dj10
cd ~/dj10/dj10/
mkdir templates
echo hello > templates/index.html
如何配置
~/dj10/dj10/
这样django将提供
~/dj10/dj10/templates/index.html
当我得到
/index.html
?

我试着编辑
~/dj10/dj10/url.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'),
]
from django.shortcuts import render

def index(request):
    context = {}
    return render(request, 'index.html', context)
然后我添加了这个文件:

~/dj10/dj10/views.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'),
]
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/url.py
    /index.html
    匹配到
    视图.index
  • python在
    views.py
  • index()
    在此处查找
    index.html
    ~/dj10/dj10/templates/index.html
  • index()
    呈现
    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.
所以,我可能在
url.py
views.py
中有语法错误? 也许我应该给…添加一些语法。。。
~/dj10/dj10/settings.py
?

问题又来了:

如何配置
~/dj10/dj10/
这样django将提供
~/dj10/dj10/templates/index.html
当我得到
/index.html

我不明白您为什么要专门提供url“index.html”。您已经将Django配置为响应路径“/”提供该HTML文件,这是正确的做法。没有理由在路径中使用模板名称,最好不要这样做

如果出于某种原因确实希望执行此操作,则需要在URL.py中指定确切的文件路径:

 url(r'^index.html$', views.index, name='index'),

但正如我所说,您不应该这样做。

使用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

我按照你的建议做了尝试:从django.conf.url导入来自django.contrib的url导入来自admin。导入视图url模式=[url(r'^admin/',admin.site.url),url(r'^index.html$',views.index,name='index'),]我在浏览器中看到了这样的响应:TemplateDoesNotExist at/index.html index.html请求方法:获取请求URL:Django版本:1.9异常类型:TemplateDoesNotExist异常值:我的建议是不要这样做,但没关系。您应该显示设置的模板部分;另外,看起来你的模板目录太深了一层,它应该在基本的dj10目录中。还要注意的是,如果你想做的只是显示静态文本“hello”,那么根本没有理由使用模板;只需在您的视图中返回HttpResponse('hello')。我同意~/dj10/dj10/templates/看起来太深了。但当我运行django admin startproject django时,django告诉我:~/dj10/dj10/根据我在教程中读到的内容,我应该将代码放在~/dj10/dj10/中。是的,这就是项目设置和URL所在的位置。但是你不应该在那里创建新的文件或目录,其他的都应该更高一级,即作为该目录的同级而不是它的子级。