路由创建 (注释为更新的内容)
from django.contrib import admin
from django.urls import path
from . import views as homeViews # import home views
urlpatterns = [
path('admin/', admin.site.urls),
path('', homeViews.home, name='home'), # route: home
path('about/', homeViews.about, name='about'), # route: about
path('contact/', homeViews.contact, name='contact'), # route: contact
]
在工程目录下创建 views.py, 写入以下内容:
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'home/home.html')
def about(request):
return render(request, 'home/about.html')
def contact(request):
return render(request, 'home/contact.html')
设置模板路径
TEMPLATES = [
…
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
与aaServer并列创建目录 templates/home
home下,base.html, index.html, about.html, contact.html
<!-- -------------------- /home/base.html ------------------------ -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% block title %}
<title>base title</title>
{% endblock %}
</head>
<body>
<p>
<a href="/">Home</a>
<a href="/about/">About</a>
<a href="/contact/">Contact</a>
</p>
{% block content %}
<h1>TO BE REPLACED...</h1>
{% endblock %}
</body>
</html>
<!-- -------------------- /home/index.html ------------------------ -->
{% extends 'home/base.html' %}
{% block title %}
<title>Main Home</title>
{% endblock %}
{% block content %}
<h1>Main home page.</h1>
{% endblock %}
<!-- -------------------- /home/about.html ------------------------ -->
{% extends 'home/base.html' %}
{% block title %}
<title>About</title>
{% endblock %}
{% block content %}
<h1>About...</h1>
{% endblock %}
<!-- -------------------- /home/contact.html ------------------------ -->
{% extends 'home/base.html' %}
{% block title %}
<title>Contact...</title>
{% endblock %}
{% block content %}
<h1>Contact...</h1>
{% endblock %