极客时间的吕老师Django课程的学习笔记
模板文件夹
创建base模板页
$mkdir templates
$cd templates/
$vi base.html
<!-- base.html -->
<h1 style="margin:auto;width:50%;">公司开放职位</h1>
<p></p>
{% block content %}
{% endblock %}
创建列表页
创建模板
(Django) 10:21 luohb@Studynode:~/Django/recruitment/jobs/templates
$ls
base.html joblist.html
(Django) 10:22 luohb@Studynode:~/Django/recruitment/jobs/templates
$cat joblist.html
{% extends 'base.html' %}
{% block content %}
终于等到你,期待加入我们,用技术去探索一个新世界
{% if job_list %}
<ul>
{% for job in job_list %}
<li>{{job.type_name}} <a href="/job/{{ job.id }}/" style="color:blue">{{ job.job_name }}</a> {{ job.city_name }} </li>
{% endfor %}
</ul>
{% else %}
<p>No jobs are available.</a>
{% endif %}
{% endblock %}
在视图层中加入自定义的页面
View.py是将model渲染出来的环节
可以函数定义,也能用视图的类去定义
1. 用函数定义
$cat jobs/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from jobs.models import Job
from jobs.models import Cities, JobTypes
# Create your views here.
def joblist(request):
job_list = Job.objects.order_by('job_type') #从模型的object数据库里面获取,按职位类别排序
#加载模板
template = loader.get_template('joblist.html') #用模板的加载器加载
#定义上下文
context = {'job_list':job_list}
for job in job_list:
job.city_name = Cities[job.job_city][1]
job.job_type = JobTypes[job.job_type][1]
return HttpResponse(template.render(context)) #将上下文展现给用户
在jobs应用中定义joblist的访问路径
$cat jobs/urls.py
from django.conf.urls import url
from jobs import views
urlpatterns = [
# 职位列表 定义URL路径的映射
url(r"^joblist/", views.joblist, name="Joblist") #若访问的url以joblist为开头,则跳转到views.joblist
]
在整个工程中定义joblist的访问路径
$cat recruitment/urls.py
"""recruitment URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
url(r"^", include("jobs.urls")),
path('admin/', admin.site.urls),
]
创建详情页
浅谈Django基础(HttpResponse、render、redirect)
创建模板
(Django) 16:47 luohb@Studynode:~/Django/recruitment
$cat jobs/templates/job.html
{% extends 'base.html' %}
{% block content %}
<div style="margin:auto; width:50%;">
<a href="/joblist" style="color:blue">返回职位列表</a><p></p>
{% if job %}
<div class="position_name">
<h2>岗位名称: {{job.job_name}}</h2>
城市:
{{job.city_name}}<p></p>
</div>
<hr>
<div class="position_responsibility", style="width:600px;">
<h3>职位职责</h3>
<pre style="font-size:16px">{{job.job_responsibility}}
</pre>
<p></p>
</div><br>
<hr>
<div class="position_requirement" style="width:600px;">
<h3>任职要求</h3>
<pre style="font-size:16px">{{job.job_requirement}}
</pre>
</div>
<br>
<div class="apply_position">
<input type="button" style="width:120px;background-color:lightblue;" value="申请"/>
</div>
{% else %}
<p>职位不存在</p>
{% endif %}
</div>
{% endblock %}
在视图中加入该模板进行渲染
(Django) 16:47 luohb@Studynode:~/Django/recruitment
$cat jobs/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.http import Http404
from jobs.models import Job
from jobs.models import Cities, JobTypes
# Create your views here.
def joblist(request):
job_list = Job.objects.order_by('job_type') #从模型的object数据库里面获取,按职位类别排序
#加载模板
template = loader.get_template('joblist.html') #用模板的加载器加载
#定义上下文
context = {'job_list':job_list}
for job in job_list:
job.city_name = Cities[job.job_city][1]
job.job_type = JobTypes[job.job_type][1]
return HttpResponse(template.render(context)) #将上下文展现给用户
def detail(request, job_id):
try:
job = Job.objects.get(pk=job_id)
job.city_name = Cities[job.job_city][1]
#job.job_type = JobTypes[job.job_type][1]
except Job.DoesNotExist:
raise Http404("Job does not exist")
return render(request, 'job.html', {'job':job}) #传值给view层
在应用url中注册该视图(定义访问地址)
(Django) 16:48 luohb@Studynode:~/Django/recruitment
$cat jobs/urls.py
from django.conf.urls import url
from jobs import views
urlpatterns = [
# 职位列表 定义URL路径的映射
url(r"^joblist/", views.joblist, name="Joblist"), #若访问的url以joblist为开头,则跳转到views.joblist
url(r"^job/(?P<job_id>\d+)/$", views.detail, name="detail"),
]
启动服务,查看效果
略
网友评论