ArrayField没有更新?

2024-09-30 04:38:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检查是否已经完成了一堂课(通过检查是否在登录帐户上访问过一次页面)。你知道吗

我一直在检查网页是否被访问过。如果没有,则应将ID添加到已访问页面的数组中。你知道吗

我正在检查所有可能的情况(或者至少是我能想到的情况),当我必须的时候,我会添加另一个选择id访问的课程的数组。每次初始化这个数组之后,我都看到添加了课程id,但没有添加到数据库中。(如果我检查数据库或更改页面,课程id将消失)

你知道吗视图.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)

你知道吗网址.py你知道吗

from django.urls import path
from . import views

urlpatterns = [
    path('', views.lectii, name="lectii"),
    path('<int:lectie_id>', views.lectie, name="lectie")
]

凝集素-选择器.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 %}

你知道吗型号.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文档来解决这个问题,但是我没有发现任何有用的东西。你知道吗

所以我想档案没有更新,我做错了什么。我该怎么做?你知道吗


Tags: djangofromimportdividifmodelscard

热门问题