You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Profile for joshdavies

Name joshdavies
Email Address n/a
Posts1
  • Views / URL's
    Board » Django » Views
    Hey all,

    I'm really hoping someone can help me, i'm new to learning Django and struggling with the views and URL's side of things..

    I'm creating a products page which includes 3 templates, the main listsings page which i would like the at: mysite.com/products/ then you can select hardware, software etc which will loads on: mysite.com/products/hardware/ This will then open a page with all the hardware which you can then click into so this should load: mysite.com/products/hardware/productname.

    I've managed to get it working up to the mysite.com/products/hardware/ and it then displays all the products however when i click into one of the products it just reloads the same page. When i hover over the button it sais the URL is going to mysite.com/hardware/myproduct/

    In my template for the URL i'm tring to pull it through using {{ product.slug }} I'm not sure if this is the best way of doing it..

    Here is some of my code:

    //URLS
    (r'^products/$', 'product.views.producthome'),
    (r'^products/(?P<slug>[-\w]+)$', 'product.views.productlist'),
    (r'^products/(?P<slug>[-\w]+)$', 'product.views.productsingle'),


    //VIEWS
    def producthome(request):
    prod_type= ProductType.objects.filter(active=True)


    return render_to_response('product_multi.html',{
    'prod_type': prod_type
    }, context_instance=RequestContext(request))


    def productlist(request, slug):
    prod_list= Product.objects.filter(active=True, slug=slug)


    return render_to_response('product_1_col.html',{
    'prod_list': prod_list
    }, context_instance=RequestContext(request))


    def productsingle(request, slug):
    prod_single= ProductType.objects.filter(active=True, slug=slug)


    return render_to_response('single-product.html',{
    'prod_single': prod_single
    }, context_instance=RequestContext(request))



    //MODELS

    class ProductType(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(blank=True)
    description = models.TextField(blank=True)
    active = models.BooleanField()

    def __unicode__(self):
    return self.title

    @models.permalink
    def get_absolute_url(self):
    return ('producthome', (), {
    'slug': self.slug
    })

    class Product(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(blank=True)
    description = models.TextField(blank=True)
    product_category = models.ForeignKey(ProductType, null=True, blank=True)
    active = models.BooleanField()


    def __unicode__(self):
    return self.name

    @models.permalink
    def get_absolute_url(self):
    return ('productlist', (), {
    'slug': self.slug
    })



    Really hope someone can help!

    Thanks,

    Josh


Powered by Sphene Community Tools