Django 如何将对象传递到TemplateView?
我只是在学习CBV,在将对象传递到TemplateView时遇到了困难。这是相当令人沮丧的,因为我知道这应该是非常基本的 以下是我的视图.py:Django 如何将对象传递到TemplateView?,django,django-templates,Django,Django Templates,我只是在学习CBV,在将对象传递到TemplateView时遇到了困难。这是相当令人沮丧的,因为我知道这应该是非常基本的 以下是我的视图.py: from __future__ import absolute_import from django.views import generic from company_account.models import CompanyProfile class CompanyProfileView(generic.TemplateView): te
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()
这是我的模型.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
这里是url.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 %}
{%extends'\u layouts/base.html%}
{%block title%}公司简介{%endblock%}
{%block headline%}公司简介{%endblock%}
{%block content%}{{CompanyProfile.company_name}{%endblock%}
我错过了什么?提前感谢您的帮助。该模板无法读取型号公司档案
。在获取任何属性之前,必须先创建模型的实例
假设您有几个公司profile
的实例:
CompanyProfile.objects.get(pk=1)-->这有一个公司名称
=“阿迪达斯”
CompanyProfile.objects.get(pk=2)-->这有一个公司名称
=“Nike”
假设你想展示耐克和阿迪达斯
下面是您可以做到的:
class CompanyProfile(models.Model):
company_name = models.CharField(max_length=25)
class CompanyProfileView(views.TemplateView):
template_name = 'my_template.html'
def get_context_data(self, **kwargs):
context = super(CompanyProfileView, self).get_context_data(**kwargs)
# here's the difference:
context['company_profiles'] = CompanyProfile.objects.all()
return context
然后,按如下方式渲染模板:
{% for company in company_profiles %}
{{ company.company_name }}
{% endfor %}
我希望这有帮助 模板无法读取模型公司档案
。在获取任何属性之前,必须先创建模型的实例
假设您有几个公司profile
的实例:
CompanyProfile.objects.get(pk=1)-->这有一个公司名称
=“阿迪达斯”
CompanyProfile.objects.get(pk=2)-->这有一个公司名称
=“Nike”
假设你想展示耐克和阿迪达斯
下面是您可以做到的:
class CompanyProfile(models.Model):
company_name = models.CharField(max_length=25)
class CompanyProfileView(views.TemplateView):
template_name = 'my_template.html'
def get_context_data(self, **kwargs):
context = super(CompanyProfileView, self).get_context_data(**kwargs)
# here's the difference:
context['company_profiles'] = CompanyProfile.objects.all()
return context
然后,按如下方式渲染模板:
{% for company in company_profiles %}
{{ company.company_name }}
{% endfor %}
我希望这有帮助 有几件事你做错了,但是为了帮助你,我们需要知道你到底在努力实现什么?有几件事你做错了,但是为了帮助你,我们需要知道你到底在努力实现什么?它成功了!这真的很有帮助。上下文让我困惑,但在你的帮助下我现在明白了。谢谢,成功了!这真的很有帮助。上下文让我困惑,但在你的帮助下我现在明白了。非常感谢。