Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

python - Mark as Read for a page in Django

I followed Sentdex Django tutorials to build a simple website. Now I want to add mark as read for each tutorial. See the image attached. Can you suggest how should I approach?

class blogCategory(models.Model):
    blog_category = models.CharField(max_length=200)
    image = models.ImageField (upload_to ='static/images/models')
    category_summary = models.CharField(max_length=200)
    category_slug = models.CharField(max_length=200)

    class Meta:
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.blog_category

class blogSeries(models.Model):
    blog_series = models.CharField(max_length=200)
    image = models.ImageField (upload_to ='static/images/models')
    blog_category = models.ForeignKey(blogCategory, default=1, verbose_name = "Category", on_delete = models.SET_DEFAULT)
    series_summary = models.CharField(max_length=200)

    class Meta:
        verbose_name_plural = "Series"

    def __str__(self):
        return self.blog_series

class blog(models.Model):
    completed=models.BooleanField(default=False)
    blog_title = models.CharField(max_length=200)
    image = models.ImageField (upload_to ='static/images/models',blank=True)
    blog_contents = models.TextField(default="")
    blog_publishedDate = models.DateTimeField("Date Published",default=datetime.now())
    blog_series = models.ForeignKey(blogSeries, default=1, verbose_name="Series", on_delete=models.SET_DEFAULT)
    blog_slug = models.CharField(max_length=200, default=1)
    def __str__(self):
        return self.blog_title

enter image description here

question from:https://stackoverflow.com/questions/65641461/mark-as-read-for-a-page-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to achieve this you need to do some database and view shenenigans, first off you'll have to check if every page of your specific Tutorial received a GET method from the specific user, that is quite simple, then you'll have to update your blog model to not show completed as a Boolean but to actually store the users that have read that tutorial completely, that can be done by creating a m2m relationship to your User model and then afterwards checking if the user is present in that field to see if the user has read your tutorial.

The better approach would probably be to create each tutorial page as an object with a m2m field called something like read_by and reference a blog for it in a ForeignKey relationship, then everytime a user is accessing the view that is tied to that page do something like this:

def tutorial_page(request, pk):
    page = Page.objects.get(pk=pk) # query the page
    blog = page.tutorial # get the tutorial the page belongs to
    if request.method == 'GET':
        page.read_by.add(request.user)
        page.save()
        for page in blog.page_set.all() # loop through each page of the tutorial to see if they are all read 
            if request.user not in page.read_by.all():
                break # if one of the pages isn't read break the loop
            else:
                blog.read_by.add(request.user) # add the user to the *read_by* field in the *blog*
                blog.save()
    return render(request, 'page.html', {'page': page})
    

In this view when the request.user is accessing the page the block of code under the if request.method == 'GET' line is being executed, I commented the code so you could understand it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...