如果满足某些条件,如何 return 关系数据库中的值;姜戈

How to return values from Relational Databases if certain criteria is met; Django

我在呈现一个视图 (myprojects) 时遇到困难,该视图呈现用户所属的项目,该项目包含在 uProjects 中。我创建了一个 object_list 来过滤 uProjects 以查看 user=user 是否将此列表分配给上下文并返回 render(request, 'myprojects.html', context)。然后我在 html 中创建了一个 for 语句来列出所有项目。这是我的代码(我列出了模型、视图、文件结构和 html,如果您需要更多,请告诉我。):

models.py:

class Project(models.Model):
    name = models.CharField(max_length=30)
    #owner = models.ForeignKey(User, on_delete=models.CASCADE, null = True)
    bPic = models.ImageField(default='defaultproban.jpg', upload_to='project_banner')

    class Meta:
        verbose_name_plural= "projects"

    def __str__(self):
        return self.name

class uProjects(models.Model):
   
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="u")
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="uProj")
    ifAccepted = models.BooleanField(null = True, blank=False, default=False)
    #ifLeader = models.BooleanField(null = False, blank=False)
    ifAdmin = models.BooleanField(null = True, blank=False, default=False)
    title = models.CharField(max_length=100, null=False, blank=False)
    def __str__(self):
        return self.user.username + ',' + self.project.name

views.py:

@login_required
def myProjects(request):
    
    object_list=uProjects.objects.filter(user=user) 
    context = {
        'object_list': object_list,
    }
    return render(request, 'myprojects.html', context)

myprojects.html

{% extends "main/base.html" %}

{%block title %}My Projects{% endblock %}

{% block content %}
    <ul>
    {% for project in object_list %}
     <li>
        Project: {{project.name}}, of {{project.department}} Department. <a href="/project/{{project.name}}/">View Details.</a> 
     </li>
    {% endfor %}
</ul>
{% endblock %}

main/base.html 减去 css:

<!doctype html>
<html>
<head>
    <div style ="background-color: #fc9842;background-image: linear-gradient(315deg, #fc9842 0%, #fe5f75 74%);">
    </style>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
{% block title %}
    <title>
    
    Candle

    </title>
{% endblock %}

</head>

<body>
    
    <div class="input-group rounded">
        <form action="{% url 'search_results' %}" method="get" >
            <input type="search" class="form-control rounded" placeholder="Search" aria-label="Search"
                aria-describedby="search-addon" name="q"/>
            
        </form>
    </div>
    <div id="content" name="content" class="main">  
        <div class="row justify-content-center">
            <div class="col-8">
                <div style="clear: both">
                    <h1 style="float: left" class="mt-2">Candle</h1>
                    <img src="https://img.icons8.com/color/48/fa314a/firebase.png"/>
                    <i><h6 style="float: right" style="color:black;font-size: 5px;">Ignite your passion.</h6></i>
                    <hr class="mt-0 mb-4">
                </div>
                {% block content %}
                {% endblock %}
                </div>
            </div>
        </div>
    </div>
    <div class="sidenav">
    {%if user.is_authenticated %}
        <a href="/">Home</a>
        <a href="/search">Search</a>
        <a href="/profile">My Profile</a>
        <a href="/notifications">Notifications</a>
        <a href="/myprojects">My Projects</a>
        <a href="/createproject">Create Project</a>
        <a href="/logout">Logout</a>
        
    
    {% else %}
        <a href="/register">Sign Up</a>
        <a href="/login">Login</a>
    {% endif %}
    </div>
    

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>

</body>
</html>

文件结构

C:.
├───.idea
│   ├───inspectionProfiles
│   └───shelf
│       └───Uncommitted_changes_before_Merge_at_13-Mar-21_15_59_[Default_Changelist]
├───AIAD
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───comments
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───departments
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───directMessage
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───friends
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───main
│   ├───migrations
│   │   └───__pycache__
│   ├───Templates
│   │   └───main
│   └───__pycache__
├───media
│   ├───AIAD
│   ├───department_banner
│   ├───department_logo
│   ├───post_image
│   ├───profile_pics
│   ├───project_banner
│   ├───project_logo
│   └───uploads
├───mysite
│   └───__pycache__
├───Notifications
│   ├───migrations
│   │   └───__pycache__
│   ├───templates
│   └───__pycache__
├───posts
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───projects
│   ├───migrations
│   │   └───__pycache__
│   ├───templates
│   │   ├───projects
│   │   └───search
│   └───__pycache__
├───reactions
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───teams
│   ├───migrations
│   │   └───__pycache__
│   └───__pycache__
├───uploads
└───users
    ├───migrations
    │   └───__pycache__
    ├───Templates
    │   ├───registration
    │   ├───search
    │   ├───templatetags
    │   └───users
    └───__pycache__

当前错误是页面未加载。如果您需要更多信息,请告诉我。

问题是您在访问模板中的 uProjects 时试图使用 Project 中的属性,因为在您看来,您正在检索 uProjects 的列表发出 uProjects.objects.filter(user=user).

myprojects.html

{% extends "main/base.html" %}

{%block title %}My Projects{% endblock %}

{% block content %}
    <ul>
    {% for uproj in object_list %}
     <li>
        Project: {{uproj.project.name}}, of {{uproj.project.department}} Department. <a href="/project/{{uproj.project.name}}/">View Details.</a> 
     </li>
    {% endfor %}
</ul>
{% endblock %}

您还应该在视图中将用户设置为 request.user

views.py:

@login_required
def myProjects(request):
    user = request.user
    object_list=uProjects.objects.filter(user=user) 
    context = {
        'object_list': object_list,
    }
    return render(request, 'projects/myprojects.html', context)