python - Porting Angular to DJango Web (1.0) style: Calling functions from Django Template -


i new django , task need accomplish port of angularjs old school web 1.0 django application (for older browsers). there 1 unknown dealing , functions associated $scope in angular. don't know how replicate logic in django.

an example (in angular):

<div ng-show="isauthorizedas('administrator')"></div> 

and in controller:

$scope.isauthorizedas = function(functionalrole) {      if($scope.user.role == 'basic'){         if(functionalrole == 'basic') return true      }      else if($scope.user.role == 'advanced'){         if(functionalrole == 'basic') return true         if(functionalrole == 'advanced') return true      }      else if($scope.user.role == 'administrator'){         if(functionalrole == 'basic') return true         if(functionalrole == 'advanced') return true         if(functionalrole == 'administrator') return true      }       return false } 

the gist controller either has complex logic (that works on 1 model or multiple models) or special formatting. realize these may non-optimal use-cases , above javascript function leaves lot desired- both objectives in direct path of execution.

my goal build functionality out within week - , don't want t deviate angularjs templates/application. therefore willing concede non-optimal solution delivery - , reconsider @ future date.

thank you

i'm not entirely sure problem is. you're looking check if logged in user admin , if render part of template. it's 1 of basic things learn while learning django. still, here how can achieve above scenario.

i have no idea user model looks have not mentioned it. i'm assuming user model has field called role can set either basic, advanced , administrator. when login django, current logged in user object available in template inside variable called user, provided have django.contrib.auth.context_processors.auth in template_context_processors settings in settings.py.

then in template can -

{% if user.role == 'administrator' %} <div class="secret_div">     super secret information here </div> {% endif %} 

if user model doesn't have such field , have other ways of checking if user admin, can add method user object subclassing default user class or creating own user model.

class user(abstractbaseuser, permissionsmixin):     """     username, password , email required. other fields optional.     """     username = models.charfield(_('username'), max_length=30, unique=true,         help_text=_('required. 30 characters or fewer. letters, digits , '                     '@/./+/-/_ only.'),         validators=[             validators.regexvalidator(r'^[\w.@+-]+$', _('enter valid username.'), 'invalid')         ])     first_name = models.charfield(_('first name'), max_length=30, blank=true)     last_name = models.charfield(_('last name'), max_length=30, blank=true)     email = models.emailfield(_('email address'), unique=true)      # more fields here       class meta:         verbose_name = _('user')         verbose_name_plural = _('users')      def get_role(self):         """         returns role of user         """         return magic_method_to_check_role(self) 

then before can

{% if user.get_role == 'administrator' %} <div class="secret_div">     super secret information here </div> {% endif %} 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -