I want that when the user logs in to the app, it should get all the details after he adds the details. like wallets name, api keys, its username etc. but its neither showing error nor it prints the data.
THis is the wallet (2nd) model code
class Wallet(models.Model):
EXCHANGES = (
('Binance', 'Binance'),
('ByBit', 'ByBit'),
('Coinbase', 'Coinbase'),
('OKX', 'OKX'),
('Bitfinex', 'Bitfinex'),
('Gemini', 'Gemini'),
)
owner = models.ForeignKey('auth.User', related_name='wallets', on_delete=models.CASCADE)
exchange_name = models.CharField(max_length=30, choices=EXCHANGES, null=True)
api_key = models.CharField(max_length=255, null=True, blank=False, validators=[MinLengthValidator(64)])
secret_key = models.CharField(max_length=255, null=True, blank=False, validators=[MinLengthValidator(64)])
wallet_type = models.CharField(max_length=30,
choices=(('spot', 'Spot'), ('futures', 'Futures'), ('margin', 'Margin')),
default='Futures')
date_connected = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.wallet_type
this is the User’s model
class UserProfile(models.Model):
@property
def getter_signal_limit(self):
return self.signal_limit
user = models.OneToOneField(User, on_delete=models.CASCADE)
# phone_number = models.CharField(max_length=20, blank=True, null=True)
# username = models.ForeignKey(Users.username, on_delete=models.CASCADE, null=False)
# wallet = models.ForeignKey('Wallet', on_delete=models.SET_NULL, null=True, blank=True)
# Telegram = models.ForeignKey('Telegram', on_delete=models.SET_NULL, null=True, blank=True)
webhook_url = serializers.SerializerMethodField() # "https://www.tradify.com/api/" + hashlib.sha256(str(User.username).encode()).hexdigest()
signal_limit = models.IntegerField(default=50, validators=[MinValueValidator(1)])
signals_used = models.IntegerField(default=0)
plan = models.CharField(max_length=12, choices=(
('Basic', 'Basic'), ('standard', 'Standard'), ('premium', 'Premium'), ('platinum', 'Platinum')),
default='Basic')
created = models.DateTimeField(auto_now_add=True, blank=True)
timezone = timezone.get_current_timezone()
this is the view that I’m using
class DashboardView1(LoginRequiredMixin, TemplateView):
template_name = 'dashboard.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Fetch the User instance based on the username
user = User.objects.get(username='hasnain')
# Fetch the UserProfile associated with the user
user_profile = UserProfile.objects.get(user=user)
# Fetch wallets and signals associated with the user
wallets = Wallet.objects.filter(owner=user_profile)
signals = Signal.objects.filter(owner=user_profile)
context['user_profile'] = user_profile
context['wallets'] = wallets
context['signals'] = signals
return context
this is the serializer’s code
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = '__all__'
# fields = ('user', 'signal_limit', 'signals_used', 'plan', 'created', 'webhook_url')
# read_only_fields = ('user', 'created')
def create(self, validated_data):
user_data = validated_data.pop('user')
user = UserSerializer.create(**user_data)
user_profile = UserProfile.objects.create(user=user, **validated_data)
return user_profile
this is the html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<!-- Add your CSS stylesheets or links to frameworks here -->
</head>
<body>
<header>
<h1>Welcome, {{ user_profile.user.username }}</h1>
<h1>Email: {{ user_profile.user.email }}</h1>
<!-- Add any other user-related information here -->
</header>
<section>
<h2>Plan:</h2>
<p> {{ user_profile.plan }}</p>
<br>
<h2>Wallets1</h2>
<ul>
{% for wallets in wallet %}
<li> wallet name {{ wallet.exchange_name }} - Type: {{ wallet.wallet_type }}</li>
<li> api key {{ wallet.api_key }}</li>
<li> secret key {{ wallet.secret_key }}</li>
<li> date connected {{ wallet.date_connected }}</li>
<!-- Display other wallet details as needed -->
{% endfor %}
</ul>
</section>
<section>
<h2>Wallets</h2>
<ul>
{% for wallet in wallets %}
<li> Wallet name {{ wallet.exchange_name }} - Type: {{ wallet.wallet_type }}</li>
<li> API key {{ wallet.api_key }}</li>
<li> Date connected {{ wallet.date_connected }}</li>
<!-- Display other wallet details as needed -->
{% endfor %}
</ul>
</section>
<br>
<br>
<!-- Add more sections or elements as necessary -->
<footer>
<!-- Add footer content if needed -->
</footer>
<!-- Add your JavaScript files or links to frameworks here -->
</body>
</html>
this is the image of the result. (data is blurred)
Image
i have tried using
Wallet.objects.filter(owner=user_profile)
and
Wallet.objects.get(owner=user_profile)
but none of them is working