when i use the function based view it works fine. can anyone help me?
this is the model
class UserProfile(models.Model):
@property
def getter_signal_limit(self):
return self.signal_limit
user = models.OneToOneField(User, on_delete=models.CASCADE)
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 user serializer 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 Function based view
login_required
def dashboard(request):
user_profile = request.user.userprofile
context = {
'user_profile': user_profile,
'signal_limit': user_profile.signal_limit,
'signals_used': user_profile.signals_used,
'plan': user_profile.plan,
'webhook_url': user_profile.get_webhook_url(),
'timezone': user_profile.timezone,
}
return render(request, 'dash.html', context)
and here is its html code
<!-- dashboard.html -->
<h1>Dashboard</h1>
<p>Username: {{ user_profile.user.username }}</p>
<p>Signal Limit: {{ signal_limit }}</p>
<p>Signals Used: {{ signals_used }}</p>
<p>Plan: {{ plan }}</p>
<p>Webhook URL: {{ webhook_url }}</p>
<p>Timezone: {{ timezone }}</p>
while this code is not showing any data of the user
Class based view:
class DashboardView(LoginRequiredMixin, TemplateView):
template_name = 'dashboard.html'
def get(self, request, **kwargs):
user_profile = request.user
context = {
# user_profile = self.request.user.userprofile
'user_profile': user_profile,
'signal_limit': user_profile.signal_limit,
'signals_used': user_profile.signals_used,
'plan': user_profile.plan,
'webhook_url': user_profile.get_webhook_url(),
'timezone': user_profile.timezone,
}
return render(self.request, self.template_name, context)
and 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>
<p>Email: {{ user_profile.user.email }}</p>
<p>Plan: {{ user_profile.plan }}</p>
<!-- Add any other user-related information here -->
</header>
<section>
<h2>Wallets</h2>
<ul>
{% for wallet in wallets %}
<li>{{ wallet.exchange_name }} - Type: {{ wallet.wallet_type }}</li>
<!-- Display other wallet details as needed -->
{% endfor %}
</ul>
</section>
<section>
<h2>Signals</h2>
<ul>
{% for signal in signals %}
<li>{{ signal.strategy_name }} - Symbol: {{ signal.symbol }}</li>
<!-- Display other signal details as needed -->
{% endfor %}
</ul>
</section>
<!-- 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>
it should show all the data user entered while signing up and rest of the entities in the model.
if anyone provides some link or source for learning django in depth, it would be helpful as i am a beginner