python - Django: Restrict access to page -
in django project have products
.
you can access products using url pattern:
example.com/tshirt
or example.com/jacket
etc.
moreover, can view other pages products, i.e.:
example.com/tshirt/details
or example.com/jacket/buy
my problem: when create new product in admin interface, product deactivated default (i can activate later). reason when create new product don't have necessary information yet (i.e. price). long product deactivated urls starting example.com/this-one-product
should not visible normal visitor (getting 404 error). unfortunately, don't know how that.
my goal it's visible super user or users staff. because makes sense can check product , see how looks when it's rendered. but, said, should not visible normal visitor of webpage.
now of course create if statements in views (and check if superuser or staff), not sound elegant solution. because have add statements many different views , that's against dry , don't solution.
what perfect: setting deliver 404 error visitors of product deactivated. how possible?
you define 2 functions return queryset visible request.user's type (staff/non-staff)
i define queryset method
def staff_visible(self): queryset = self.filter(q(staff_visible=true)) return queryset def visible(self): queryset = self.filter(is_active=true) return queryset
now use product.objects.staff_visible()
, on.
furthermore define 2 custom manager class , override get_queryset
use either of above two.
Comments
Post a Comment