python - Getting profile data user without additional model -


i have user customize model several roles or users types such as:

  • student user
  • professor user
  • executive user

my user model of way:

class user(abstractbaseuser, permissionsmixin):     email = models.emailfield(unique=true)     username = models.charfield(max_length=40, unique=true)     slug = models.slugfield(         max_length=100,         blank=true     )      is_student = models.booleanfield(         default=false,         verbose_name='student',         help_text='student profile'     )     is_professor = models.booleanfield(         default=false,         verbose_name='professor',         help_text='professor profile'     )     is_executive = models.booleanfield(         default=false,         verbose_name='executive',         help_text='executive profile',     )      other fields ... 

i have functions user model too, let me profiles according user type:

    def get_student_profile(self):         student_profile = none         if hasattr(self, 'studentprofile'):             student_profile = self.studentprofile         return student_profile      def get_professor_profile(self):         professor_profile = none         if hasattr(self, 'professorprofile'):             professor_profile = self.professorprofile         return professor_profile      def get_executive_profile(self):         executive_profile = none         if hasattr(self, 'executiveprofile'):             executive_profile = self.executiveprofile         return executive_profile      # method not works. it's illustrative     # how user profile in view send data?      def get_user_profile(self):         user_profile = none         if hasattr(self, 'user'):             user_profile = self.user         return user_profile      def save(self, *args, **kwargs):     user = super(user,self).save(*args,**kwargs)      # creating user student profile     if self.is_student , not studentprofile.objects.filter(user=self).exists():         student_profile = studentprofile(user = self)         student_slug = self.username         student_profile.slug = student_slug         student_profile.save()      # creating user professor profile     elif self.is_professor , not professorprofile.objects.filter(user=self).exists():         professor_profile = professorprofile(user=self)         professor_slug = self.username         professor_profile.slug = professor_slug         professor_profile.save()      # creating user executive profile     elif self.is_executive , not executiveprofile.objects.filter(user=self).exists():         executive_profile = executiveprofile(user = self)         executive_slug = self.username         executive_profile.slug = executive_slug         executive_profile.save()  # have signal username , assign slug field  @receiver(post_save, sender=user) def post_save_user(sender, instance, **kwargs):     slug = slugify(instance.username)     user.objects.filter(pk=instance.pk).update(slug=slug) 

each profile (student, professor , executive) managed in own model own fields:

class studentprofile(models.model):     user = models.onetoonefield(         settings.auth_user_model,         on_delete=models.cascade     )  class professorprofile(models.model):      user = models.onetoonefield(         settings.auth_user_model,         on_delete=models.cascade     )  class executiveprofile(models.model):      user = models.onetoonefield(         settings.auth_user_model,         on_delete=models.cascade     ) 

in authentication process, i've set login_redirect_url = 'dashboard' attribute, in settings, , have dashboardprofileview class ask profile users when these students or professors or executives of way:

class dashboardprofileview(loginrequiredmixin, templateview):     template_name = 'dashboard.html'      def get_context_data(self, **kwargs):         context = super(dashboardprofileview, self).get_context_data(**kwargs)         user = self.request.user         if user.is_student:             profile = user.get_student_profile()             context['userprofile'] = profile          elif user.is_professor:             profile = user.get_professor_profile()             context['userprofile'] = profile          elif user.is_executive:             profile = user.get_executive_profile()             context['userprofile'] = profile          elif user.is_study_host:             profile = user.get_study_host_profile()             context['userprofile'] = profile          elif user.is_active:             profile = user.get_user_profile()             context['userprofile'] = profile          return context 

my dashboardprofileview class works well, when user authenticated have is_student, or is_professor or is_executive attribute.

but when create , user without of these attributes (is_student, or is_professor or is_executive) user profile context in dashboardprofileview not sent template, reason not values profile of user.

as user not have attribute profile, ask of way in view:

if user.is_active:     profile = user.get_user_profile()     context['userprofile'] = profile 

due users created have is_active boolean attribute set true, not work.

how can user profile in view when user not is_student, not is_professor , not is_executive, mean, normal django user?

update

according @sijan bhandari answer it's true, context['userprofile'] = self.request.user suited way of ask normal user profile. should works.

but these suited change in base template have noreversematch problem in of urls , this:

file "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/urls/resolvers.py", line 392, in _reverse_with_prefix     (lookup_view_s, args, kwargs, len(patterns), patterns) django.urls.exceptions.noreversematch: reverse 'preferences' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['accounts/preferences/(?p<slug>[\\w\\-]+)/$'] [07/apr/2017 11:52:26] "get /dashboard/ http/1.1" 500 179440    

dashboard/ it's url redirect when user signin.

url(r'^dashboard/', dashboardprofileview.as_view(), name='dashboard'), 

but problem it's when system try render accounts/preferences/username url, created view account options users without matter user type.

in urls.py y have 2 urls accounts/

url(r'^accounts/', include('accounts.urls', namespace = 'accounts')), # call accounts/urls.py other urls preferences/  url(r'^accounts/', include('django.contrib.auth.urls'), name='login'), # don't assign namespace because django url pre-bult-in login function 

in accounts/urls.py have preferences/ url:

url(r"^preferences/(?p<slug>[\w\-]+)/$",     views.accountsettingsupdateview.as_view(),     name='preferences' ), 

here, telling request user slug field make reference username field. pass in url slug of user retrieve account data , want regular expression let me alphanumeric characters in uppercase , lowercase , hypen , underscores

then in template, put url of way:

<li><a href="{% url 'accounts:preferences' userprofile.user.username %}">settings</a></li> 

but when sign in process user not have (is_student, is_professor, is_executive) profiles, mean django normal user, have following behavior:

enter image description here

file "/home/bgarcial/.virtualenvs/ihost/lib/python3.5/site-packages/django/urls/resolvers.py", line 392, in _reverse_with_prefix     (lookup_view_s, args, kwargs, len(patterns), patterns) django.urls.exceptions.noreversematch: reverse 'preferences' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['accounts/preferences/(?p<slug>[\\w\\-]+)/$'] [07/apr/2017 12:12:18] "get /dashboard/ http/1.1" 500 179440 


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -