4👍
It turns out that there is a dj-stripe solution to this already (that I hadn’t found in my Google searches).
I just added this to my extended user model:
def __str__(self):
return self.username
def __unicode__(self):
return self.username
@cached_property
def has_active_subscription(self):
"""Checks if a user has an active subscription."""
return subscriber_has_active_subscription(self)
And then added this to my template:
{% if request.user.has_active_subscription %}
<a href="/payments/history/">Subscription History</a>
{% else %}
<a href="/payments/subscribe/">Upgrade to Premium Content!</a>
{% endif %}
1👍
I agree with @nnaelle that adding an is_subscribed
attribute to your user model is a good option for ensuring that the subscribe button isn’t shown to your users that are already subscribed. Adding this attribute means that, if you haven’t already, you’ll need to extend the user model in your models.py
to keep track of whether they are subscribed. This looks something like this (as in the Django docs):
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
is_subscribed = models.BooleanField(default=False)
The new user model can then be accessed by, say, the views in views.py
:
from my_app.models import UserProfile
def index(request):
user = request.user
userprofile = UserProfile.objects.get(user=user)
...
You can (and might want to) avoid having to access the user profile that way, however, by making your new user model the default for your app under settings.py
as described in the docs for substituting custom user models. Then after passing in the new user model to the view, you can do a check for subscription status using {% if not userprofile.is_subscribed %}
and only show the subscription button for those who aren’t subscribed:
{% if not userprofile.is_subscribed %}
<div ...></div>
{% endif %}
Feel free to give me your feedback!
- [Django]-Deploying Django with gunicorn No module named ImportError: No module named validation
- [Django]-Django Rest Framework update() with kwargs from validated_data
- [Django]-Django Queryset Filter Missing Quotes
- [Django]-Python / Django – Exception Value: 'WSGIRequest' object has no attribute 'Meta'
- [Django]-SuspiciousOperation when loading image in django