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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…