0
votes

I am new to Python Django and I am struggling with URL patterns,My creating websites which has three Html pages and their three respective views,I am attchaing the images of My code and project structure Please help me to find the solution: Project Structure:- enter image description here

So idea is when i click on index.html the page gets redirect to detail.html with image id like 'music/id' now i want to redirect to other html page on image click in detail.html to picturedetail.html with 'song_title' attribute

Code:-

  1. Models.py:-

    from django.db import models
    
    
    
    class Album(models.Model):
      art_type=models.CharField(max_length=255)
      album_title=models.CharField(max_length=255)
      art_method=models.CharField(max_length=255)
      album_logo=models.FileField()
    
    def __str__(self):
        return self.album_title;
    
    
    class Picture(models.Model):
      album=models.ForeignKey(Album,on_delete=models.CASCADE)
      file_type=models.FileField()
      song_title=models.CharField(max_length=245)
    
  2. detail.html:-

      {% extends 'music/base.html'  %}
      {% block title %}Album Details{% endblock %}
      {% block body %}
      <ul>
         {% for picture in album.picture_set.all %}
          <div class="col-sm-4 col-lg-2">
               <div class="thumbnail">
                  <a href="{% url 'music:picturedetail' picture.song_title %}">
                   <img src="{{ picture.file_type.url }}" class="img-responsive">
                            </a>
                            <div class="caption">
                              <h6>{{picture.song_title}}</h6>
                            </div>
                    </div>
    
            </div>
    
         {% endfor %}
      </ul>
    

    {% endblock %}

  3. urls.py:-

    from django.conf.urls import url from . import views

    app_name='music';
    urlpatterns = [
    
        url(r'^$',views.IndexView.as_view(),name='index'),
    
        # /music/id/
        url(r'^(?P<pk>[0-9]+)$',views.DetailView.as_view(),name='detail'),
        #for PictureDetail view
        url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),
    
    ]
    

4.view.py:-

from django.views import generic
from .models import Album
from .models import Picture



class IndexView(generic.ListView):
    template_name = 'music/index.html'
    context_object_name = 'all_albums'
    def get_queryset(self):
        return Album.objects.all()

class DetailView(generic.DetailView):
     model=Album
     template_name = 'music/detail.html '


class PicturedetailView(generic.DetailView):
    model =Picture
    template_name = 'music/picturedetail.html'

The Error i am getting is:-

  NoReverseMatch at /music/1
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Request Method: GET
Request URL:    http://127.0.0.1:8000/music/1
Django Version: 2.0.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'picturedetail' with arguments '('River',)' not found. 1 pattern(s) tried: ['music/(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)']
Exception Location: C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-2.0.6-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 636
Python Executable:  C:\Users\dell\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.1
Python Path:    
['D:\\Django\\website',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-2.0.6-py3.6.egg',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\pytz-2018.4-py3.6.egg']
Server time:    Fri, 6 Jul 2018 03:00:51 +0000
1

1 Answers

2
votes
url(r'^(?P<pk>[0-9]+)/(?P<song_title>[A-Za-z]+)', views.PicturedetailView.as_view(), name='picturedetail'),

This has two arguments, pk and song_title. Try the following:

{% url 'music:picturedetail' pk=picture.pk song_title=picture.song_title %}