Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. from django.shortcuts import render_to_response, get_object_or_404
  2. from django.http import HttpResponsePermanentRedirect
  3. from django.core.urlresolvers import reverse
  4.  
  5. def product(request, slug):
  6. # Product ID where we start 301 Redirects
  7. redirect_before = 15
  8.  
  9. # If slug is numeric, find the product from Product.id
  10. if slug.isdigit():
  11. product = get_object_or_404(Product, id=int(slug))
  12.  
  13. # If the slug isn't numeric, we find the product from Product.slug
  14. else:
  15. product = get_object_or_404(Product, slug=slug)
  16.  
  17. # If the Product.id is equal to or less than redirect_before,
  18. # we 301 Redirect to our new URL
  19. if product.id <= redirect_before:
  20. return HttpResponsePermanentRedirect(reverse('shoppingcart-product', args=[product.slug,]))
  21.  
  22. return render_to_response('shoppingcart/product.html', {'product':product})
Add Comment
Please, Sign In to add comment