On 5 Feb., 09:21, Ken Chida <ken.ch...@gmail.com> wrote: > I tried my best to search for an answer but my efforts yielded nothing. > Allow me to give you a simple example to illustrate my problem. Let's > pretend that I want to have a login form on every single page on my > website. Obviously, the login form will have a corresponding view > function. What is the most elegant way to implement the login functionality > on every single page? One brute force method would be to include the login > view inside all view functions for every page; these view functions would > call the login view if the HttpRequest object contains a flag that becomes > active when the user clicks the login submit button. This method would > work, but it goes against DRY best practices. > > Is there a better way to do this? Maybe I can use a custom template tag? > > Thanks in advance!! > Ken
Hello Ken, I somehow feel the most elegant way would be a wrapper function like django's @login_required. However, I've once tried that myself and it didn't work as I wanted, so I'm not really sure what exactly you would have to do. You could also define a function that gets the request (and maybe a data-dict) as variables and adds its result (the login-form) to the data-dict, like: def _login_part(request, data={}): #the login-part of your view that leads to login_form = ... data.update({'login_form': login_form}) return data def your_view(request): data = _login_part(request) # ... return render_to_response('xxx.html', RequestContext(request, data)) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.