1

I've been trying to use multiple DetailView's in my views.py but due to some reason I'm getting this error during template rendering. http://prntscr.com/7f8vql http://prntscr.com/7f8vlv .

My views.py

def rock_and_feat(request):
    feats = Feat.objects.order_by('-created')[:3]
    rocks = Rockinfo.objects.order_by('-rank')[:50]
    context = RequestContext(request, {
    'feats': feats, 'rocks': rocks
    })
    return render_to_response('template.html', context)


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

class RockDetailView(generic.DetailView):
    model = Rockinfo
    template_name = 'feature/detailrock.html' 
    context_object_name = 'rockinfo'

My apps urls.py

urlpatterns = [

url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/$', views.RockDetailView.as_view(),  name='detailrock'),
]

My project's urls.py

urlpatterns = [
url(r'^', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

template.html

   {% block rocker %}
      {% if rocks %}
          {% for item in rocks %}

              <div class="artist-container">
              <a href="{% url 'feature:detailrock' rockinfo.id %}"><img src="{{ item.rock_img.url }}" class="artist-img" alt="Dimitri Vegas &amp; Like Mike">
               <div class="artist-info">{{ item.rock_name }}</div></a></div>
           {% endfor %}
       {% else %}
               <p>No rockers are available.</p>
       {% endif %}
              </div></div>
   {% endblock %}

I can't figure out what the error is about.Thanks

WutWut
  • 1,204
  • 2
  • 17
  • 31

3 Answers3

3

I rememeber your question about rock_and_feat.

There are several things I want to mention:

urlpatterns = [
  url(r'^$', views.rock_and_feat, name='rock_and_feat'),
  url(r'^detail/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
  url(r'^rock_detail/(?P<pk>[0-9]+)/$', views.RockDetailView.as_view(), name='detailrock'),
]

You don't have rockinfo. It is item. In this line:

{% url 'feature:detailrock' item.id %}

One more thing. Note, that id is not the same as pk.

{% url 'feature:detailrock' item.pk %}

One last thing. This can be replaced:

   {% if rocks %}
      {% for item in rocks %}
          ....
      {% endfor %}
   {% else %}
       <p>No rockers are available.</p>
   {% endif %}

With this (docs):

   {% for item in rocks %}
       ...
   {% empty %}
       <p>No rockers are available.</p>
   {% endfor %}
Community
  • 1
  • 1
sobolevn
  • 16,714
  • 6
  • 62
  • 60
0

(wild guess here) Reading the error, it seems to me that rockinfo.id is empty or it doesn't exists (or not passed to the template), have you checked that?

Besides that, you should have two different patterns for the views

Hope this helps

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
0

1.Please check template for getting rockinfo.id and check.

<a href="{% url 'feature:detailrock' rockinfo.id %}">

2.Remove namespace from urlconf and just use <a href="{% url 'detailrock' rockinfo.id %}"> in template.

I think either 1 or 2 will work.