Home > Blockchain >  How to subtract from a Django model?
How to subtract from a Django model?

Time:07-10

I am trying to add and subtract 1 from the value of a Django model's IntegerField and display it on the webpage, depending on when a button is clicked.

models.py

class User(AbstractUser):
    following = models.ManyToManyField("self", related_name="followers", symmetrical=False)
    following_num = models.IntegerField(default=0)

In views.py:

To add: following_num = page_visitor.following_num 1

To subtract: following_num = page_visitor.following_num -1

This is displaying in the html where the number should be displaying:

<django.db.models.query_utils.DeferredAttribute object at 0x0000022868D735E0>

entire view:

def username(request, user):
    #get user
    user = get_object_or_404(User.objects, username=user)
    posts = Post.objects.filter(user=user).order_by('-date_and_time')
    page_visitor = get_object_or_404(User.objects, username=request.user)

    if user == request.user:
        followButton = False
    else:
        followButton = True

    if request.method == "POST":
        if "follow" in request.POST:
            request.user.following.add(user)
            following_num = page_visitor.following_num  1
            #following_num = F('User.following_num')   1

        elif "unfollow" in request.POST:
            request.user.following.remove(user)
            following_num = page_visitor.following_num -1
            #following_num = F('User.following_num') - 1

    followers_num = page_visitor.following_num
    following_num = User.following_num
    return render(request, "network/username.html",{
        "user": user,
        "posts": posts,
        "followButton": followButton,
        "followers": followers_num,
        "following": following_num
    })

html

{% block body %}
    <h1>{{ user }}</h1>
    <h4>Followers:{{ followers }}</h4>
    <h4>Following:{{ following }}</h4>
    <!--follow/unfollow button-->
    {% if followButton == True %}
    <form action = "{% url 'username' user %}" method = "POST">
        {% csrf_token %}
        {% if user not in request.user.following.all %}
            <input type="submit" value="Follow" name="follow">
        {% else %}
            <input type="submit" value="Unfollow" name="unfollow">
        {% endif %}
    </form>
    {% endif %}
    

    <!--displays all the posts-->
    {% for post in posts %}
            <div class = "individual_posts">
                <a href="{% url 'username' post.user %}"><h5 class = "post_user">{{ post.user }}</h5></a>
                <h6 id = "post_itself">{{ post.post }}</h6>
                <h6 class = "post_elements">{{ post.date_and_time }}</h6>
                <h6 class = "post_elements">{{ post.likes }} Likes</h6>
            </div>
    {% endfor %}
{% endblock %}

CodePudding user response:

you reference the class (model) and not the object (instance)

following_num = User.following_num

as you alredy passed your user object to the template you can also access the attributes directly {{user.following_num }} {{request.user.following_num }}

but you better rename the passed user variable to avoid confusion / errors

Get user information in django templates

CodePudding user response:

Without the html that is supposed to display the number I cant tell why it is displaying that, but if you intend to change the following count then you need to call page_visitor.save() after changing a property of page_visitor in order for it to save the new property's value in the database

  • Related