Reputation: 3
error: >> braintree_client_token = gateway.client_token.generate({"customer_id": request.user.userprofile.agent_id}) **
RelatedObjectDoesNotExist at /account User has no userprofile. *Request Method: GET *Request URL: http://127.0.0.1:8000/account *Django Version: 3.1.7 *Exception Type: RelatedObjectDoesNotExist *Exception Value: User has no userprofile.
- Sorry, can someone help me?
This is my problem. I don’t understand what is missing to become User have no Profile...., can you find out my problem? Or is there any relevant information for me? The solutions on the Internet are not suitable for me. Can you give me some direction to think about the problem? I have been unable to solve it for two days. I know that User has uesername / first_name / last_name / Mail is there any way to solve the problem?
///Version:///
asgiref==3.3.1
braintree==4.6.0
certifi==2020.12.5
chardet==4.0.0
Django==3.1.7
idna==2.10
pytz==2021.1
requests==2.25.1
six==1.15.0
sqlparse==0.4.1
urllib3==1.26.4
///settings.py ///
INSTALLED_APPS = [
'api.apps.ApiConfig',
# 'api'
'users.apps.UsersConfig'
# 'users'
]
/// views.py ///
Account view for registered users
@login_required
def account(request):
braintree_client_token = gateway.client_token.generate({"customer_id": request.user.userprofile.agent_id})
braintree_data = BraintreeData(request.user)
context = {
"braintree_client_token": braintree_client_token,
"invoices": braintree_data.invoices(),
}
#passes 'verified' parameter to url to handle a success message
verified = request.GET.get("verified", None)
if verified:
context["verified"] = "true"
else:
context["verified"] = "false"
return render(request,'users/account.html', context)
///models.py///
from django.contrib.auth.models import User
# from django.db.models.signals import post_save
# from django.dispatch import receiver
'''
Our UserProfile model extends the built-in Django User Model
'''
class UserProfile(models.Model):
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile')
telephone = models.CharField(max_length=15, null=True, blank=True)
address = models.CharField(verbose_name="Address", max_length=100, null=True, blank=True)
town = models.CharField(verbose_name="Town/City", max_length=100, null=True, blank=True)
county = models.CharField(verbose_name="County", max_length=100, null=True, blank=True)
post_code = models.CharField(verbose_name="Post Code", max_length=8, null=True, blank=True)
country = models.CharField(verbose_name="Country", max_length=100, null=True, blank=True, default="UK")
is_active = models.BooleanField(default=True)
agent_id = models.CharField(max_length=100, null=True, blank=True)
def __str__(self):
return self.user
# if not created:
# return UserProfile.objects.create(user=instance)
# post_save.connect(create_profile, sender=User)
///
ps:
▲I originally hoped that post_save could be used to solve the problem, but there was no action at all.Tried post_save but it didn't get resolved.
///
/// user/urls.py ///
from django.conf.urls import url
from django.urls import path
from . import views
app_name = "users"
urlpatterns = [
path('', views.sign_in, name="sign-in"),
url(r'^sign-up', views.sign_up, name="sign-up"),
url(r'^sign-out', views.sign_out, name="sign-out"),
url(r'^forgotten-password', views.forgotten_password, name="forgotten-password"),
url(r'^account', views.account, name="account"),
url(r'^verification/(?P<uidb64>.+)/(?P<token>.+)/$',views.verification, name='verification'),
]
/// forms.py ///
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, SetPasswordForm, PasswordResetForm
from django.contrib.auth.models import User
from django import forms
from .models import UserProfile
'''
Form that uses built-in UserCreationForm to handel user creation
'''
class UserForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=True,
widget=forms.TextInput(attrs={'placeholder': '*Your first name..'}))
last_name = forms.CharField(max_length=30, required=True,
widget=forms.TextInput(attrs={'placeholder': '*Your last name..'}))
username = forms.EmailField(max_length=254, required=True,
widget=forms.TextInput(attrs={'placeholder': '*Email..'}))
password1 = forms.CharField(
widget=forms.PasswordInput(attrs={'placeholder': '*Password..','class':'password'}))
password2 = forms.CharField(
widget=forms.PasswordInput(attrs={'placeholder': '*Confirm Password..','class':'password'}))
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'password1', 'password2', )
Upvotes: 0
Views: 196
Reputation: 306
request.user.userprofile.agent_id
request.user is an Instance of User. You are trying to access the userprofile using the related_name which is mentioned on UserProfile table
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile')
In order to to access the userprofile from user. The user must have a related object on UserProfile. In your case the user does not have a related object in UserProfile.
To test, create a UserProfile from admin and whatever user you select while creating UserProfile. Login with the same user. The error will go away.
Upvotes: 1