I have a basic question which can be useful for new Django developers.
I created my own UserProfile in Django. This UserProfile has a specific field called 'type'. This field can have two values (until now maybe more in the future) : Male - M / Female - F :
from django.contrib.auth.models import User
GENDER = (
(M, 'Male'),
(F, 'Female'),
)
class UserProfile(models.Model):
user = models.OneToOneField(User)
type = models.CharField( max_length=2,
choices=GENDER,
default='F')
Basically, I wanted to allow access to restrict access or to adapt page content depending on user Type. Until now, I used a really basic and beginner approach which is to test user type in a view and then process the page:
def OnePage(request):
if request.user.type == 'M':
....
else if request.user.type =='F':
....
Then I also need to adapt the template rendered depending on user type: a male user will not have the same profile page that a Female User.
I am sure there are better ways to do this but as a Django beginner I am quite lost with all of Django possibilities. So if you have any best practices to implement this please tell me (obviously I would like a DRY code I could use on every view!)
Thank you for your help.