[Answered ]-Django serve get-requests

1👍

So your syntax for get_object_or_404 is wrong. You don’t pass it an object: it gets the object for you. So:

user = get_object_or_404(User, email=email)

Now you’ve got a User instance, and you want to get the relevant profile, so you can just do:

 profile = user.userprofile

Alternatively it might be easier to grab the profile directly, if you don’t need the actual user instance for anything else:

 profile = get_object_or_404(UserProfile, user__email=email)

Now you can check the relevant attributes:

 osusername == profile.osusername
 computername == profile.computername

1👍

You need to retrieve the User instance first by:

try:
    a_user = User.objects.get(email=email)
except User.DoesNotExist:
    # error handling if the user does not exist

Then, get the corresponding UserProfile object by:

profile = a_user.userprofile

Then, you can get osusername and computername from the UserProfile object:

profile.osusername
profile.computername
👤Cheng

0👍

As an addition to @daniel-roseman answer.

If checking the relevant attributes is a common task on multiple views it could also be worth creating a method in your UserProfile model which can perform the required validation check.

class UserProfile(object):
    # various attributes ...

    def check_machine_attributes(self, os_username, computer_name):
        if os_username == self.osusername and computername == self.computername:
            return True
        return False

In your view you can then do:

if profile.check_machine_attributes(osusername, computername):
    # ...
else:
    # ...

Leave a comment