Reputation: 5136
Actually I'm try to get product details but when I go to 127.0.0.1:8000/mobiles/motorola-moto-g-16gb it is loading templates but showing nothing and even doesn't showing any error.
Note: 'mobiles' is category in url and rest is slug in Product models that you can see down.
models.py
from django.db import models
from django.utils.text import slugify
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
def __unicode__(self):
return self.name
class Product(models.Model):
title = models.CharField(max_length=140)
slug = models.SlugField(null = True, blank = True)
imgurl = models.CharField(max_length=500)
price = models.CharField(max_length=100)
category = models.ForeignKey(Category)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(Product, self).save(*args, **kwargs)
def __unicode__(self):
return self.title
views.py
from django.shortcuts import render_to_response, get_object_or_404, Http404
from django.template import RequestContext
from cat.models import Category, Product
def detail(request, slug, category_name_url):
try:
category = Category.objects.get(name=category_name_url)
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
raise Http404
return render_to_response('product/detail.html', {'product': product, 'category_name': category_name_url})
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from cat import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<category_name_url>\w+)/$', views.category, name='category'),
url(r'^(?P<category_name_url>\w+)/(?P<slug>[-\w]+)$', views.detail , name='detail'),
)
product/detail.html
<h1>{{ object.title }}</h1>
<p> {{ object.price }}</p>
<img src="{{ object.imgurl }}">
Upvotes: 0
Views: 303
Reputation: 726
Since you are passing 'product' in your context, you have to use 'product' in your templates.
return render_to_response('product/detail.html', {'product': product, 'category_name': category_name_url})
So, the template code should be:
<h1>{{ product.title }}</h1>
<p> {{ product.price }}</p>
<img src="{{ product.imgurl }}">
Upvotes: 2