我只是在學習CBV,並且在將對象傳遞給TemplateView時遇到困難。這已經非常令人沮喪,因爲我知道這應該是非常基本的。如何將對象傳遞給TemplateView?
這裏是我的views.py:
from __future__ import absolute_import
from django.views import generic
from company_account.models import CompanyProfile
class CompanyProfileView(generic.TemplateView):
template_name = 'accounts/company.html'
def get_context_data(self, **kwargs):
context = super(CompanyProfileView, self).get_context_data(**kwargs)
return CompanyProfile.objects.all()
這裏是我的Models.py:
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class CompanyProfile(models.Model):
company_name = models.CharField(max_length=255)
def __str__(self):
return self.company_name
這裏是urls.py
urlpatterns = [
url(r'^accounts/companyprofile/$', CompanyProfileView.as_view()),
]
最後,這裏是模板:
{% extends '_layouts/base.html' %}
{% block title %}Company Profile{% endblock %}
{% block headline %}<h1>Company Profile</h1>{% endblock %}
{% block content %}{{ CompanyProfile.company_name }}{% endblock %}
我錯過了什麼?預先感謝您的幫助。
你做錯了什麼事情很少,但爲了幫助你,我們需要知道你究竟想要達到什麼目的? –