Django 职位开发系统 03,04 ---匿名用户查看 (吕召刚的课程)

为了让匿名用户查看职务信息,需要完成如下的项目:

  1. 在recruitment里面建立templates文件夹
  2. 在templates文件夹类建立base.html,并编写
  3. 在templates文件夹中建立joblist.html,并编写
  4. 修改views.py
    5.在jobs中建立urls.py, 注册joblist
  5. 在project 页面下urls.py,注册jobs.urls

启动访问

1. 在jobs里面建立templates文件夹

2. 在templates文件夹类建立base.html,并编写


浆果科技的开放职位

{% block content %} {% endblock %}

3. 在templates文件夹中建立joblist.html,并编写


{% extends 'base.html' %}


{% block content %}

    终于等到你,期待加入我们,用技术去探索一个新的世界

{% if job_list %}
    
    {% for job in job_list %}
  • {{ job.type_name }} {{ job.job_name }} {{ job.city_name }}
  • {% endfor %}
{% else %}

No jobs are available.

{% 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
然后可以看到下面的页面

image.png

作为一个小白,我感悟一下。
太难了。正事因为难,所以要去尝试,多学习。
获得几个不同的技能,然后,让技能叠加。

纯小白,不懂前端,不懂后端,不懂数据库,不懂交互,不懂java,不懂javascript,不懂css,啥也不懂。 python只会点皮毛。
很多大佬们认为很简单的东西,我们却要用很就才能搞定。


04 显示职位详情

实现查看详情页面,需要如下几步:

  1. 在jobs/templates 中添加job.html, 然后修改这个页面
  2. 修改jobs/view.py , 定义job的详细展示页面
  3. 在jobs/urls.py, 注册职位的详细展示信息
    (作为不懂前端的我,手动敲入job.html 的页面出错的可能性非常大)

1. 在jobs/templates 中添加job.html, 然后修改这个页面

我直接从copy的源代码

{% extends 'base.html' %}

{% block content %}

{% if job %}

岗位名称:{{job.job_name}}

城市: {{job.city_name}}


岗位职责:

{{job.job_responsibility}}
        

任职要求:

{{job.job_requirement}}
        
{% else %}

职位不存在

{% endif %} {% endblock %}

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\d+)/$",views.detail,name='detail'),

]

你可能感兴趣的:(Django 职位开发系统 03,04 ---匿名用户查看 (吕召刚的课程))