帮忙看看下面代码:
运行:http://127.0.0.1:8000/joblist/
会报错: File "D:\test_web2\test_web3\jobs\views.py", line 18, in joblist
job.city_name=Cities[job.job_city][1]
Django环境:4.0.5
python环境:3.8.6
具体代码如下:
\job\templates\basr.html
<h1 style="margin:auto;width:50%;">匠人阿强科技开放职位 </h1>
<p></p>
{% block content %}
{% endblock %}
\job\templates\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 %}
\job\templates\urls.py
```python
from django.urls import re_path
from jobs import views
urlpatterns=[
#职位列表
re_path(r'^joblist/',views.joblist,name='joblist')
****\job\templates\views.py****
```python
# Create your views here.
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from jobs.models import Job,Cities,JobTypes
from django.http import Http404
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.type_name=JobTypes[job.job_type][1]
return render(request, 'joblist.html', context)
# return HttpResponse(template.render(context))
\test_web3\urls.py
from django.contrib import admin
from django.urls import path,re_path
from django.conf.urls import include
urlpatterns = [
re_path('^',include('jobs.urls')),
path('admin/', admin.site.urls)
]