照着书上的代码,抄了几段。最后有个bug怎么都改不好,暂时先写到这……
# 安装 Django
wget https://www.djangoproject.com/m/releases/2.0/Django-2.0.1.tar.gztar zxvf Django-2.0.1.tar.gz
cd Django-2.0.1/
python -m pip install --upgrade pip setuptools
python -m pip install django
python -V
#用安装包装Django最后因为linux系统自带Python2 ,调用最新的Django时总是自动说Python版本太低
# 也试着找到Python3的在安装位置,直接执行其中的python,但是还是失败
sudo apt-get install python3-django -y
## 最后还是装低版本的Django:
pip install Django==1.9
# 下面是创建网页,还创建模板,讲变量写进html文件:(懒得注释太多。。。待完善)
django-admin.py startproject todos
cd todos/
python manage.py startapp todo
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("Hello world!")
from django.shortcuts import render
from django.http import HttpResponse
def hello_world(request):
return HttpResponse("""<html>
<head>
<title>My Todo list!</title>
</head>
<body>
<h1>Todos:</h1>
<p>Mow the lawn</p>
<p>Backup your PC</p>
<p>Buy some milk</p>
</body>
</html>""")
gedit todo/index.tmpl
<html>
<head>
<title>My Todo List</title>
<style type="text/css">
body{font-family:Arial,Helvetica,sans-serif;color:black;background:#ffffff;}
</style>
</head>
<body>
{% if todos %}
<table border="1">
<tr><td>Todo</td><td>Importance</td></tr>
{% for todo in todos %}
<tr><td>{{todo.title}}</td>
<td>{{todo.importance}}</td></tr>
{% endfor %}
</table>
{% else %}
<p>You have nothing to do!</p>
{% endif %}
</body>
</html>
gedit todos/settings.py
TEMPLATES = [
{
'DIRS': ['/home/twq/MySoftware/todos/todo'],
}
gedit todo/views.py
# Create your views here.
from django.shortcuts import render
from django.template import Context,loader
from django.http import HttpResponse
def hello_world(request):
todos =[{'title':'Mow the lawn','importance':'Minor'},
{'title':'Backup your PC','importance':'High'},
{'title':'Buy some milk','importance':'Medium'},]
t=loader.get_template('index.tmpl')
c=Context({
'todos':todos,
})
return HttpResponse(t.render(c))
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3',todo.db),
}
}