WutWut
WutWut

Reputation: 1234

django 1.8 using one view in another

My finalproject project has 2 apps. rocklist and feature.I want both the apps to appear on the same web page. I've searched on the topic and it seems it can be done by using both the views in one single view.(I hope that made sense). But I can't figure out how to use both rocklist and feature's views in feature's views. 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'      

Is there a way to declare both the views in a single view so that both the apps can be displayed on the same home page.I'm a beginner and couldn't find anything on the internet that could help with this issue.Thanks in advance.

Upvotes: 1

Views: 56

Answers (2)

sobolevn
sobolevn

Reputation: 18070

A quick example of using function-based views:

# 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)

And for urls:

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

This method is usefull, when you have a complex logic in your view. For example, you need to show aggregated data from several models. Read about function-based views in the docs and consider this approach in the future development.

Upvotes: 1

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

How about something simplistic like:

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 %}

You could DRY this up even further with an 'as_widget' property on the model class that returns a widget template using render_to_string et cetera et cetera..

Upvotes: 1

Related Questions