按类别显示图像

时间:2014-01-27 09:22:27

标签: python django

我想按类别显示图片,收到一点错误index() got an unexpected keyword argument 'category_id'。当我点击类别时,它会显示图像,该图像被分配到此类别。它显示了类别但是当我点击它时我得到了错误

models.py

class Image(models.Model):
    title        = models.CharField(max_length=255)
    slug         = models.SlugField(unique=True, max_length=255)
    category     = models.ForeignKey('Category')
    description  = models.CharField(max_length=255)
    published    = models.BooleanField(default=False)
    created      = models.DateTimeField(auto_now_add=True)
    image        = models.ImageField(upload_to="images")

def __unicode__(self):
    return u'%s' % self.title


 class Category(models.Model):
    title        = models.CharField(max_length=255)
    slug         = models.SlugField(unique=True, max_length=255)

def __unicode__(self):
    return u'%s' % self.title

urls.py

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<category_id>\d+)/$', views.index, name='lists'),
 )

views.py

def index(request):
    categories = Category.objects.all()
    context = {'categories': categories}
    return render(request, 'photo/index.html', context)

def lists(request):
    images = Image.objects.all().order_by('title')
    images = Image.objects.filter(published=True)
    context = {'images': images}
    return render(request, 'photo/lists.html', context)

的index.html

<h2>Categories</h2>
{% for category in categories %}
    <ul>    
        <li>
            <a href="/photo/{{ category.id }}/"> {{category.title}}</a>
        </li>
    </ul>
{% endfor %} 

lists.html

<h1>Photos</h1>
<a href="/photo/{{ image.id }}"><img src="{{ image.image.url }}" style="width:200px; height: 200px; float:left;"/></a>

2 个答案:

答案 0 :(得分:1)

models.py

class Image(models.Model):
    title        = models.CharField(max_length=255)
    slug         = models.SlugField(unique=True, max_length=255)
    category     = models.ForeignKey('Category')
    description  = models.CharField(max_length=255)
    published    = models.BooleanField(default=False)
    created      = models.DateTimeField(auto_now_add=True)
    image        = models.ImageField(upload_to="images")

def __unicode__(self):
    return u'%s' % self.title


 class Category(models.Model):
    title        = models.CharField(max_length=255)
    slug         = models.SlugField(unique=True, max_length=255)

def __unicode__(self):
    return u'%s' % self.title

urls.py

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^photo/(?P<category_id>\d+)/$', views.lists, name='lists'),
 )

views.py

def index(request):
    categories = Category.objects.all()
    context = {'categories': categories}
    return render(request, 'photo/index.html', context)

def lists(request,category_id):
    try:
        categories = Category.objects.get(pk = 'category_id')
    except:pass
    images = categories.image_set.all().order_by('title')
    context = {'images': images}
    return render(request, 'photo/lists.html', context)

的index.html

<h2>Categories</h2>
{% for category in categories %}
    <ul>    
        <li>
            <a href="/photo/{{ category.id }}/"> {{category.title}}</a>
        </li>
    </ul>
{% endfor %} 

lists.html

<h1>Photos</h1>
{%for image in images %}
    <a href="#"><img src="{{ image.image.url }}" style="width:200px; height: 200px; float:left;"/></a>
{%endfor%}

答案 1 :(得分:0)

我想通了,我不得不改变我的urls.py

    url(r'^(?P<category_id>\d+)/$', views.lists, name='lists'),