1.安装完python和django后,在想进工程的路径下用一下命令创建工程:
bash-3.2# django-admin.py startproject EasyWeb
2.输入以下命令,打开浏览器输入http://127.0.0.1:8000即可看到django页面了
bash-3.2# ls
EasyWeb manage.py
bash-3.2# python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 23, 2018 - 13:41:29
Django version 2.1, using settings 'EasyWeb.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
3.然后输入以下命令创建工程app:bash-3.2# django-admin.py startapp mainsite
bash-3.2# ls
EasyWeb db.sqlite3 mainsite manage.py
bash-3.2# cd mainsite
bash-3.2# ls
__init__.py apps.py models.py views.py
admin.py migrations tests.py
4.去settings.py的INSTALLED_APPS添加上app
bash-3.2# ls
__init__.py __pycache__ settings.py urls.py wsgi.py
bash-3.2# vi settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]
5.在settings.py的DIRS添加路径TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
6.创建templates以及index.htmlbash-3.2# ls
EasyWeb db.sqlite3 mainsite manage.py
bash-3.2# mkdir templates
bash-3.2# cd templates
bash-3.2# vi index.html
bash-3.2# cd ..
bash-3.2# ls
EasyWeb db.sqlite3 mainsite manage.py templates
bash-3.2#
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'/>
</head>
<body>
<h1>welcome to my web</h1>
<h1>now time: {{ now }}</h1>
</body>
</html>
7.去views.py中关联上index.html:from django.shortcuts import render
from django.template.loader import get_template
from django.http import HttpResponse
from datetime import datetime
# Create your views here.
def homepage(request):
template = get_template('index.html')
now = datetime.now()
html = template.render(locals())
return HttpResponse(html)
8.打开urls.py导入views.py的homepage:bash-3.2# cd easyweb
bash-3.2# ls
__init__.py __pycache__ settings.py urls.py wsgi.py
bash-3.2# vi urls.py
from django.contrib import admin
from django.conf.urls import include, url
from mainsite.views import homepage
urlpatterns = [
url(r'^$', homepage),
]
9.再次运行python3 manage.py run server
打开127.0.0.1:8000即可看到如下: