Django创建网页--主页及模板
程序员文章站
2022-07-08 17:24:42
1. 创建主页使用Django创建网页的过程通常分三个阶段:定义URL、编写视图和编写模板每个URL都被映射到特定的视图,视图函数获取并处理网页所需的数据2.编写更多视图现在在views.py 里添加更多视图...
1. 创建主页
- 使用Django创建网页的过程通常分三个阶段:定义URL、编写视图和编写模板
- 每个URL都被映射到特定的视图,视图函数获取并处理网页所需的数据
2.编写更多视图
-
现在在
views.py
里添加更多视图
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Hello, Welcome to Home Page")
def project_list(request):
return HttpResponse('You\'re looking at the list of project list')
def register(request):
return HttpResponse('You\'re looking at the register page')
-
把这些新视图添加进
urls
模块里
# 作者:伊洛Yiluo 公众号:伊洛的小屋
# 个人主页:https://yiluotalk.com/
# 博客园:https://www.cnblogs.com/yiluotalk/
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='index'),
path('project_list/', views.project_list, name='project list'),
path('register/', views.register, name='register'),
]
-
分别输入网址查看结果
3. 写一个真正有用的视图
-
每个视图必须要做的只有两件事:返回一个包含被请求页面内容的 [
HttpResponse
]对象,或者抛出一个异常,比如 [Http404
] -
回到
views.py
-
之前我们在管理后台添加过project的数据
- 来修改一下主页的代码
from django.http import HttpResponse
from .models import ProjectInfo
# Create your views here.
def home(request):
project_list = ProjectInfo.objects.order_by('add_data')[:5]
output = ', '.join([q.project_name for q in project_list])
return HttpResponse(output)
def project_list(request):
return HttpResponse('You\'re looking at the list of project list')
def register(request):
return HttpResponse('You\'re looking at the register page')
-
打开网页查看
内容已经成功的展示
4.模板
- 虽然上面实现了暂时页面,但是存在一个比较严重的问题。内容是写在代码里面的,这样不适合管理。下面创建一个视图,就可以将页面的设计从代码中分离出来
-
首先来创建一个
templates
目录,用来存放静态文件,再创建一个目录 autoapi,然后在其中新建一个文件home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AutoPlarform Home</title>
</head>
<body>
{% if project_list %}
<ul>
{% for project in project_list %}
<li>
<a href="/autoapi/register/">{{ project.project_name }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
-
下面在更新下
views.py
from django.http import HttpResponse
from django.template import loader
from .models import ProjectInfo
# Create your views here.
def home(request):
project_list = ProjectInfo.objects.order_by('add_data')[:5]
template = loader.get_template('autoapi/home.html')
context = {
'project_list': project_list,
}
return HttpResponse(template.render(context, request))
def project_list(request):
return HttpResponse('You\'re looking at the list of project list')
def register(request):
return HttpResponse('You\'re looking at the register page')
-
我们来打开主页查看下有没有效果
- 信息成功的显示在了主页上
5.一个快捷函数: render()
-
Django 提供了一个快捷函数,我们用它来重写
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import ProjectInfo
# Create your views here.
def home(request):
project_list = ProjectInfo.objects.order_by('add_data')[:5]
context = {'project_list': project_list}
return render(request, 'autoapi/home.html', context)
def project_list(request):
return HttpResponse('You\'re looking at the list of project list')
def register(request):
return HttpResponse('You\'re looking at the register page')
-
再次刷新主页查看一下效果
- 同刚才是一个效果
本文地址:https://blog.csdn.net/weixin_44386231/article/details/108258476