我正在尝试检查课程是否已经完成(通过检查登录帐户是否曾经访问过该页面)。
我一直在尝试检查是否已访问该页面。如果没有,则应将ID添加到已访问页面的数组中。
我正在检查所有可能的情况(或者至少是我能想到的情况),并且在必要时,我向访问的课程数组中添加了另一个lection.id。每次使该数组优先化之后,我看到课程ID被添加了,但是没有添加到数据库中。 (如果我检查数据库或更改页面,则课程ID消失)
views.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from .models import Lectie
def lectii(req):
lectii = Lectie.objects.all().order_by("id")
if req.user.is_authenticated:
if req.user.profile.lectiiRezolvate == None:
lectiiRezolvate=[]
else:
lectiiRezolvate = req.user.profile.lectiiRezolvate
else:
lectiiRezolvate=[]
context = {
'lectii': lectii,
'lectiiRezolvate': lectiiRezolvate
}
print(lectiiRezolvate)
return render(req, '../templates/pagini/lectii-selector.html', context)
def lectie(req, lectie_id):
if req.user.is_authenticated:
if req.user.profile.lectiiRezolvate == None:
req.user.profile.lectiiRezolvate.append(lectie_id)
user.profile.save()
print(req.user.profile.lectiiRezolvate)
else:
if lectie_id in req.user.profile.lectiiRezolvate:
pass
else:
req.user.profile.lectiiRezolvate.append(lectie_id)
print(req.user.profile.lectiiRezolvate)
else:
pass
lectie2 = get_object_or_404(Lectie, pk=lectie_id)
lectiePDF = 'lectii/lectia-{}.pdf'.format(lectie2)
context = {
'lectiePDF': lectiePDF,
'lectie2': lectie2,
}
return render(req, '../templates/pagini/lectii.html', context)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.lectii, name="lectii"),
path('<int:lectie_id>', views.lectie, name="lectie")
]
lectii-selector.html
{% extends 'base.html' %}
{% block content %}
</div>
<div id="middle-section" class="container-fluid container-fluid-margin">
{% for lectie in lectii %}
<div class="row content-block" style="padding-top: 2rem;">
<div class="col-md-12 text-card">
<div class="card card-custom">
{% if lectie.id in lectiiRezolvate %}
<div class="card-body">
<h5 class="card-title">Capitolul {{lectie.capitol}}<i class="fas fa-check"></i></h5>
<p class="card-text">{{lectie.cardText}}</p>
<a href="{% url 'lectie' lectie.id %}" class="btn btn-primary">Reia</a>
</div>
{% else %}
<div class="card-body">
<h5 class="card-title">Capitolul {{lectie.capitol}}</h5>
<p class="card-text">{{lectie.cardText}}</p>
<a href="{% url 'lectie' lectie.id %}" class="btn btn-primary">Începe</a>
</div>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
帐户中的models.py(我已经扩展了默认用户模型)
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.postgres.fields import ArrayField
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
xp = models.IntegerField(default=0)
lectiiRezolvate = ArrayField(models.IntegerField(), null=True, blank=True)
exercitiiRezolvate = ArrayField(models.IntegerField(), null=True, blank=True)
exercitiiProvocari = ArrayField(models.IntegerField(), null=True, blank=True)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
我觉得我没有正确地添加到数据库中,并且缺少了一些东西。我尝试阅读django文档来解决该问题,但没有发现任何有用的信息。
所以我想个人资料没有更新,我做错了什么。我该如何运作?
答案 0 :(得分:1)
仅当数组字段以前为空白时才保存配置文件;您不会在添加新ID后保存它。
(也请注意,如果在字段定义中添加default=list
,则可以避免检查None。)