new_progger_python
new_progger_python

Reputation: 29

How to create a rating user django

I have a mini blog and user profiles. On the user profiles page, I display information:

Comments count of this user; 
Posts count of this user;

I display the number of comments in the template userprofile like this:

{{user.comments.count}}

I would like to make a small rating system, for example:

If a user has left 10 comments and 10 posts - he is a cat (cat- word).
If 20 is a dog (dog - word).
30 - elephant, etc.

For this I would like to have a separate table in the database, so that I can then make a selection by users rating.

How can I implement this?

Upvotes: 0

Views: 308

Answers (1)

SLDem
SLDem

Reputation: 2182

Take a look at this post: Range as dictionary key in Python

Creating a seperate model and assigning user titles as a relation would lead to way to many database transitions, here you can just grab the value of user posts with count and then use a custom made dictionary with range object as a key to assign he correct title

Update

To implement you would do something like this:

def user(request, pk):
    user = User.objects.get(pk=pk)
    count_of_posts = user.posts.count()
    titles = {
        range(1, 10): 'Cat',
        range(10, 20): 'Dog',
        range(20, 30): 'Elephant'
        # add more titles
    }
    user_title = ''
    for key in titles:
        if count_of_posts in key:
            user_title = titles[key]
    return render(request, 'user.html', {'user': user,
                                         'user_title': user_title})

Update 2

Well since you want to add tags to a user list there isn't really a way around it except creating a seperate field for it in a User model and updating it with the information everytime a user creates or deletes a post, here's how I see it working:

1.Add a title field to your User model.

models.py

class User(models.Model): # your user model with your inheritance, disregard the 'models.Model' part, its just an example
    # your User model fields
    title = models.CharField('Title', max_length=50)

2. In your views when you create (or delete) a post, check the current users posts count and if the value changed assign the new title and save it.

views.py

titles = { # add this dictionary seperately in your views file
    range(1, 10): 'Cat',
    range(10, 20): 'Dog',
    range(20, 30): 'Elephant'
    # add more titles
}

def index(request): # the view that handles post creation 
    if request.method == 'POST':
        form = PostForm(request.POST) # your post form class
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.user = request.user
            new_post.save()
            count_of_posts = request.user.posts.count()

            for key in titles: # assign a title to the user and save it
                if count_of_posts in key:
                    if request.user.title != titles[key]:
                        request.user.title = titles[key]
                        request.user.save()
    else:
        form = PostForm()
    return render(request, 'index.html', {'form': form})


 def delete_post(request):
     # create a similar check but call it when deleting a post


 def user_list(request): #template below
     users = User.objects.all()
     return render(request, 'user_list.html', {'users', users})


 def user(request, pk): # template below
     user = User.objects.get(pk=pk)
     return render(request, 'user.html', {'user': user})
     

3. Create your templates.

user_list.html

{% for user in users %}
    {{ user }} is a {{ user.title }}
{% endfor %}

user.html

{{ user }} is a {{ user.title }}

Upvotes: 1

Related Questions