django 1.8在另一个视图中使用一个视图

时间:2015-06-08 18:34:40

标签: python django

我的finalproject项目有2个应用。 rocklistfeature。我希望这两个应用都出现在同一个网页上。我已经搜索了这个主题,似乎可以通过在一个视图中使用这两个视图来完成。(我希望这是有意义的)。 但我无法弄清楚如何在rocklist的观看中使用featurefeature的观看次数。 rocklist models.py

from django.db import models


class Rockinfo(models.Model):
  rock_name = models.CharField(max_length=200,default="ac/dc")
  rock_img = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
  rank = models.IntegerField(default=0)


  def __str__(self):              
    return self.rock_name


class Rockvids(models.Model):
  rockername = models.ForeignKey(Rockinfo)
  vid_id = models.CharField(max_length=200,default="Z7JgY9zezj4")
  vid_name = models.CharField(max_length=300,default="Something Inside Me")
  vid_singer_name = models.CharField(max_length=300,default="Jonathan Reyes Myers")

  def __str__(self):
     return self.vid_id

feature models.py

from django.db import models

class Feat(models.Model):
 feat_name = models.CharField(max_length=200,default="feature1")
 feat_img1 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
 feat_img2 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
 feat_img3 = models.ImageField(upload_to="%Y/%m/%d",default="1992/08/92")
 feat_vid1 = models.CharField(max_length=200,default="3NQRhE772b0")
 feat_vid2 = models.CharField(max_length=200,default="XU3h3CVI_gI")
 feat_vid3 = models.CharField(max_length=200,default="vqHIQD4_lu4")
 num = models.IntegerField(default=0)


 def __str__(self):              
     return self.feat_name

feature views.py

from django.models.rocklist import Rockinfo, Rockvids


class IndexView(generic.ListView):
 template_name = 'feature/base.html'
 context_object_name = 'latest_feat_list'

 def get_queryset(self):
    """Return the last five published questions."""
    return Feat.objects.order_by('-num')[:1]

 class DetailView(generic.DetailView):
  model = Feat
  template_name = 'feature/detail.html' 

rocklist views.py

from .models import Rockvids, Rockinfo


class IndexView(generic.ListView):
  template_name = 'rockinglist/index.html'
  context_object_name = 'latest_rockinfo_list'

 def get_queryset(self):

    return Rockinfo.objects.order_by('-rank')[:50]


 class DetailView(generic.DetailView):
 model = Rockinfo
 template_name = 'rockinglist/detail.html'      

有没有办法在一个视图中声明两个视图,以便两个应用程序都可以显示在同一个主页上。我是初学者,无法在互联网上找到任何可以帮助的内容有这个问题。谢谢。

2 个答案:

答案 0 :(得分:1)

如此简单的事情如何:

<强> views.py

class MyTemplateView(TemplateView):
    template_name = 'myapp/mytemplate.html'

    def get_context_data(self, request, **kwargs):
        context = super(MyView, self).get_context_data(request, **kwargs)
        context['widget_1'] = ModelFoo.objects.all()
        context['widget_2'] = ModelBar.objects.all()
        return context

<强>的myapp / mytemplate.html

{% if widget_1 %}
    <ul class="widget widget_1">
        {% for item in widget_1 %}
            <li>{{ item }}</li>
        {% endfor %}
    </ul>
{% endif %}

你可以使用DRY等等返回小部件模板的模型类上的'as_widget'属性进一步render_to_string这样做。

答案 1 :(得分:1)

使用基于函数的视图的简单示例:

# your_app/views.py
def rock_and_feat(request):
    feats = Feat.objects.order_by('-num')[:1]
    rocks = Rockinfo.objects.order_by('-rank')[:50]
    context = RequestContext(request, {
        'feats': feats, 'rocks': rocks
    })
    return render_to_response('template.html', context)

对于网址:

# deprecated in 1.8:
# urlpatterns = patterns('',
urlpatterns = [
    # Example:
    url(r'^rock_and_feat/$', app.views.rock_and_feat, name='rock_and_feat'),
]
# )

当您在视图中有复杂的逻辑时,此方法很有用。例如,您需要显示来自多个模型的聚合数据。 阅读基于函数的视图in the docs并在未来的开发中考虑这种方法。