我不确定哪里出错了,但是我的模板没有渲染。我相信我已经在以下Python文件中设置了所有正确的代码,但看不到它可以呈现。如果这很重要,我正在使用postgresql:
urls.py :
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
import jobs.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', jobs.views.home, name='home')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py :
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'jobs.apps.JobsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
views.py
from django.shortcuts import render
from .models import Job
def home(request):
jobs = Job.objects
return render(request, 'jobs/home.html', {'jobs': jobs})
models.py :
from django.db import models
class Job(models.Model):
image = models.ImageField(upload_to = 'images/')
summary = models.CharField(max_length = 200)
jobs / templates / jobs / home.html :
<div class="album py-5 bg-light">
<div class="container">
<div class="row">
{% for i in jobs.all %}
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img src="" alt="">
<div class="card-body">
<p class="card-text">{{ jobs.summary }}test</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
答案 0 :(得分:1)
jobs
是QuerySet
个对象的Job
,因此它没有summary
属性。在这里,您使用i
作为jobs
的迭代器。最好将其重命名为job
,然后使用以下命令呈现它:
<div class="album py-5 bg-light">
<div class="container">
<div class="row">
{% for job in jobs.all %}
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img src="" alt="">
<div class="card-body">
<p class="card-text">{{ job.summary }}test</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</div>