Django 职位开发系统 03,04 ---匿名用户查看 (吕
2021-07-02 本文已影响0人
死也要学会django
为了让匿名用户查看职务信息,需要完成如下的项目:
- 在recruitment里面建立templates文件夹
- 在templates文件夹类建立base.html,并编写
- 在templates文件夹中建立joblist.html,并编写
- 修改views.py
5.在jobs中建立urls.py, 注册joblist - 在project 页面下urls.py,注册jobs.urls
启动访问
1. 在jobs里面建立templates文件夹
2. 在templates文件夹类建立base.html,并编写
<!-- base.html -->
<h1 style = "margin:auto;width:50%;">浆果科技的开放职位</h1>
<p></p>
{% block content %}
{% endblock %}
3. 在templates文件夹中建立joblist.html,并编写
<!-- 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.</p>
{% endif %}
{% endblock %}
4. 修改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')
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))
5.在jobs中建立urls.py, 注册joblist
from django.conf.urls import url
from jobs import views
urlpatterns = [
#职位列表
url(r"^joblist/", views.joblist, name="joblist"),
]
6. 在project 页面下urls.py,注册jobs.urls
"""recruitment URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/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),
]
启动项目,然后输入 http://127.0.0.1/joblist
然后可以看到下面的页面
作为一个小白,我感悟一下。
太难了。正事因为难,所以要去尝试,多学习。
获得几个不同的技能,然后,让技能叠加。
纯小白,不懂前端,不懂后端,不懂数据库,不懂交互,不懂java,不懂javascript,不懂css,啥也不懂。 python只会点皮毛。
很多大佬们认为很简单的东西,我们却要用很就才能搞定。
04 显示职位详情
实现查看详情页面,需要如下几步:
- 在jobs/templates 中添加job.html, 然后修改这个页面
- 修改jobs/view.py , 定义job的详细展示页面
- 在jobs/urls.py, 注册职位的详细展示信息
(作为不懂前端的我,手动敲入job.html 的页面出错的可能性非常大)
1. 在jobs/templates 中添加job.html, 然后修改这个页面
我直接从copy的源代码
{% extends 'base.html' %}
{% block content %}
<div style="margin:auto; width:50%;">
{% if job %}
<div class="position_name" z>
<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>
</div>
<hr>
<div class="position_requirement" style="width:600px; ">
<h3>任职要求:</h3>
<pre style="font-size:16px">{{job.job_requirement}}
</pre>
</div>
<div class="apply_position">
<input type="button" class="btn btn-primary" style="width:120px;" value="申请" onclick="location.href='/resume/add/?apply_position={{job.job_name}}&city={{job.city_name}}'"/>
</div>
{% else %}
<p>职位不存在</p>
{% endif %}
{% endblock %}
</div>
2. 修改jobs/view.py , 定义job的详细展示页面
注意大小写
修改的部分
#新加
from django.shortcuts import render
from django.http import Http404
# Create your views here.
#新加
def detail(request,job_id):
try:
job = Job.objects.get(pk=job_id)
job.city_name = Cities[job.job_city][1]
except Job.DoesNotExist:
raise Http404("Job does not exist!")
return render (request, 'job.html',{'job':job})
源码
from django.shortcuts import render
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')
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]
except Job.DoesNotExist:
raise Http404("Job does not exist!")
return render (request, 'job.html',{'job':job})
3. 在jobs/urls.py, 注册职位的详细展示信息
from django.conf.urls import url
from jobs import views
urlpatterns = [
#职位列表
url(r"^joblist/", views.joblist, name="joblist"),
url(r"^job/(?P<job_id>\d+)/$",views.detail,name='detail'),
]