Reputation: 170
I am having difficulty rendering a view (myprojects) that renders the projects that a user is a part of, which is contained in uProjects. I created an object_list that filters uProjects to see if user=user
assigned this list to the context and returned render(request, 'myprojects.html', context)
. Then I created a for statement in the html to list all projects. Here is my code (I've listed the models, views, file structure, and html, please let me know if you need more.):
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 minus the 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/[email protected]/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/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
</body>
</html>
file structure
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__
The current error is that the page doesn't load. Let me know if you need more information.
Upvotes: 1
Views: 108
Reputation: 4264
The problem is that your're trying to use attributes from Project
when accessing a uProjects
in the template, since on your view you're retrieving a list of uProjects
when issuing 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 %}
You should also set the user as request.user
on your view.
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)
Upvotes: 1