简体   繁体   English

向用户添加徽章以在Django中执行任务

[英]adding badges to user for doing tasks in django

Hi Djangonauts, How is everyone today, I am new to Django so please forgive any silly mistakes in logic or code. 嗨,Djangonauts,您好,我是Django的新手,所以请原谅逻辑或代码中的任何愚蠢错误。 I have a user, a User_Profile and a posts model. 我有一个用户,一个User_Profile和一个帖子模型。 I want to give the user badges for the number of posts the user posts. 我想为用户徽章提供用户发布的帖子数量。 Example if the user posts 3 posts he/she gets a beginner badge and so on... 例如,如果用户发布了3条帖子,他/她将获得初学者徽章,依此类推...

Below is my user Profile models.py 以下是我的用户个人资料models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_image = models.ImageField(default='', blank=True, null=True)
    badge_image = models.ImageField(default='', blank=True, null=True)#maybe default can be "static/images/beginner_image.jpg" but it does not work 

posts models.py 发表models.py

User = get_user_model()
class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')
    title = models.CharField(max_length=250, unique=True)  
    slug = models.SlugField(allow_unicode=True, unique=True)
    message = models.TextField()

Now I want to achieve something like this: 现在,我要实现以下目标:

 user = User.objects.get(username=request.user.username)
    if user.posts.count >= 3 :
             badge_image = '<img src="some image">'# I don't know how to make a image variable in views and models. I know how to use it in Django templates with {{image.url}}   
    elif user.posts.count >= 20 :
             badge_image = '<img src="some image">'
    else:
         badge_image ='<img src="beginner_image">'

Where do I write this code in the models, in the views? 在视图的模型中,我该在哪里编写代码? I am sorry if my questions are too basic. 如果我的问题太基础了,我感到抱歉。 None of the projects I learned had a similar scenario 我学到的所有项目都没有类似的情况

You can do it without ImageField . 您可以在没有ImageField Use model's @property for this. 为此使用模型的@property You need to copy badge images(eg badge_img1.jpg , badge_img2.jpg , etc.) to director with static files(check docs how to manage static with Django ). 您需要将带有静态文件(例如, 如何使用Django管理静态文件)的证件图像(例如badge_img1.jpgbadge_img2.jpg等)复制到director。 Rewrite Profile like this: 像这样重写Profile

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_image = models.ImageField(default='', blank=True, null=True)

    @property
    def badge_image(self):
        if self.user.posts.count() >= 20:
             badge_image = 'badge_img1.jpg'# I don't know how to make a image variable in views and models. I know how to use it in Django templates with {{image.url}}   
        elif self.user.posts.count() >= 3:
             badge_image = 'badge_img2.jpg'
        else:
             badge_image ='badge_img3.jpg'
        return badge_image

you can display current user's image like this: 您可以这样显示当前用户的图像:

{% load static %}
<img src={% static user.profile.badge_image %}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM