我正在尝试创建一个名为“服务”的应用,我一直关注Django tutorial如何创建自己的应用。
我在Django CMS中制作这个应用程序,我基本上希望这个应用程序包含模板,然后我可以通过includes
标签插入到CMS的页面中。
我制作了我的模型,视图,网址和模板。但模板未从视图中接收任何数据。我能够在后端创建新的服务对象,所以我知道我的模型正在按我的意愿工作,并且在包含它时我没有收到模板错误。
以下是我的模型,视图,网址文件和模板。我怀疑这与我如何设置视图或如何在URL文件中提取视图有关。
目前,我在模板中获得的唯一输出是“No Services Available”,当视图中没有service_list时,它将被设置为输出。
models.py
from django.db import models
from djangocms_text_ckeditor.fields import HTMLField
import filer.fields.image
from django_extensions.db.fields import AutoSlugField
from colorfield.fields import ColorField
class Service(models.Model):
title = models.CharField(max_length=200,default="")
slug = AutoSlugField(max_length=50, populate_from='title')
color = ColorField(default='#FFFFFF')
tab_content = HTMLField(blank=True,help_text='Describe the service you offer and how that might benfit people.')
page_content = HTMLField(blank=True,help_text='Describe the service you offer and how that might benfit people.')
quote = models.CharField(max_length=200,default="")
icon = filer.fields.image.FilerImageField(verbose_name='Image',blank=False,null=True,on_delete=models.SET_NULL,related_name='+',)
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.views.generic import View, ListView, DetailView, TemplateView
from .models import Service
#Create your views here.
def tabs(request):
services_list = Service.objects.all()[:5]
context = {'services_list': services_list,}
return render(request,'service_tabs.html', context)
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.tabs, name='Service Tabs'),
]
模板
{% load cms_tags staticfiles sekizai_tags %}
{% block content %}
{% if services_list %}
<!-- Nav tabs -->
<div id="services" class="container">
<ul id="services_list" class="nav nav-tabs" role="tablist">
{% for service in services_list %}
<p>{{ service.title }}</p>
{% endfor %}
</ul>
</div>
<!-- End of: Nav tabs -->
{% else %}
<p>No Services Available</p>
{% endif %}
{% endblock %}