开发者

Is it OK to do 302s for architecture in my web applciation?

开发者 https://www.devze.com 2022-12-21 05:19 出处:网络
For example, in my index(request): def index(request): if logged_in: return HttpResponseRedirect(\"/home_profile\")

For example, in my index(request):

def index(request):
    if logged_in:
        return HttpResponseRedirect("/home_profile")
    else:
        return HttpResponseRedirect("/login")

This way, when the user hits my home pag开发者_开发知识库e...he is redirected appropriately. Is this a good architecture? Or will this cause caching problems, etc?


Redirection is ok(302 shouldn't cause any caching problem, as 302's are temporary), but why you need to have redirection in both if and else. Better way is to redirect to login page if not logged-in, view should otherwise return the response, instead of unnecessarily redirecting e.g.

def home(request):
    if not  logged_in:
        return HttpResponseRedirect("/login?next=%s"%reverse("home"))

    return HttpResponse(...)

you can do same thing in each view where user needs to be logged in, else make a login decorator, djago auth already has login_required or make a login middleware which will do it for every request.

Also note I am passing the next url to visit after login, because usually I would like to land where i was heading.

0

精彩评论

暂无评论...
验证码 换一张
取 消