简体   繁体   中英

Show profile of custom user in django

hi ia working on a project for that i have made login and registration of a user. now i want to show full profile of user.since get_profile is not working anymore so how can i get full profile of a user

my models.py

class Consultants(models.Model):

    consul_id=models.IntegerField(default=0,primary_key=True)
    first_name=models.CharField(max_length=255,blank=True,null=True)
    last_name=models.CharField(max_length=255,blank=True,null=True)
    email=models.EmailField(max_length=255,blank=True,null=True)
    username=models.CharField(max_length=255,blank=True,null=True)
    password=models.CharField(max_length=50,blank=True,null=True)

    last_login=models.DateTimeField(default=datetime.now,blank=True,null=True)
    is_active=models.BooleanField(default=False)

    def __str__(self):
        return self.first_name

views.py for login and registration

def register(request):
    context = RequestContext(request)
    registered = False
    if request.method == 'POST':
        # user_form = UserForm(data=request.POST)
        consultant_form = ConsultantsForm(data=request.POST)
        if consultant_form.is_valid():
            consultant = consultant_form.save(commit=False)
            consultant.save()
            registered = True
        else:
            print consultant_form.errors
    else:
        consultant_form = ConsultantsForm()
    return render_to_response(
            'register.html',
            {'consultant_form': consultant_form, 'registered': registered},
            context_instance=RequestContext(request))

def login_user(request):
     context = RequestContext(request)
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        print type(username)
        print "username",username
        try:
            user = Consultants.objects.get(Q(username= username) & Q(password= password))
            print 'chala'
            if user.is_active:
                user.backend = 'django.contrib.auth.backends.ModelBackend'
                login(request, user)
                return HttpResponse("welcome......you are succesfuly log in")
            else:
                return HttpResponse("Your UkKonnect account is disabled.")
        except ObjectDoesNotExist:
            return HttpResponse("INvalid User")


    else:
         return render_to_response('login.html', {}, context)

i want to make def full_profile and def edit_profile. How can i get logged in user consul_id?? please help me

Not sure that I understand you problem well.. Take a look at recommended way of extending User model:

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    department = models.CharField(max_length=100)

Then you can do:

u = User.objects.get(username='fsmith')
freds_department = u.employee.department

In your case it would be:

class Consultant(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    *your fields here*
  1. Now you can use standard authentication forms and methods
  2. You always can obtain consultant data as:

     request.user.consultant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM