for 循环在 Django 模板中不起作用(在 table 标记内)
for loop doesn't work in Django template (inside of table tag)
我正在尝试在我的 html 模板中创建一个 table ,
但是当我写 <td>{{project.title}}</td>
它不起作用!
实际上当我使用 {{x}} 我没有输出!
我真的不知道我的模板里面有什么问题...
html 模板:
{% extends 'main.html' %}
{% block content %}
<h1>Projects</h1>
<table>
<tr>
<th>ID</th>
<th>Project</th>
<th>Votes</th>
<th>Ratio</th>
<th></th>
</tr>
{% for project in Projects %}
<tr>
<td>{{project.id}}</td>
<td>{{project.title}}</td>
<td>{{project.vote_total}}</td>
<td>{{project.vote_ratio}}</td>
<td>{{project.created}}</td>
<td><a href="{% url 'project' project.id %}">View</a></td>
</tr>
{% endfor %}
</table>
{% endblock content %}
models.py:
from django.db import models
import uuid
from django.db.models.deletion import CASCADE
# Create your models here.
class Project(models.Model):
title = models.CharField(max_length=200)
descripeion = models.TextField(null=True, blank=True)
demo_link = models.CharField(max_length=1000, null=True, blank=True)
source_link = models.CharField(max_length=1000, null=True, blank=True)
tags = models.ManyToManyField('Tag', blank=True)
vote_total = models.IntegerField(default=0, null=True, blank=True)
vote_ratio = models.IntegerField(default=0, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.title
class Review(models.Model):
VOTE_TYPE = (
('up', 'Up Vote'),
('down', 'Down Vote')
)
# owner =
project = models.ForeignKey(Project, on_delete=CASCADE)
body = models.TextField(null=True, blank=True)
value = models.CharField(max_length=200, choices=VOTE_TYPE)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.value
class Tag(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.name
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Project
projectsList = [
{
'id':'1',
'title':'Ecommerce Website',
'description':'Fully functional ecommerce website'
},
{
'id':'2',
'title':'Portfolio Website',
'description':'This was a project where i built out my
portfolio'
},
{
'id':'3',
'title':'Social network',
'description':'owesome open source project i am still
working'
},
]
def projects(request):
projects = Project.objects.all()
context = {'projects': projects}
return render(request, 'projects/projects.html',context)
def project(request, pk):
projectObj = Project.objects.get(id=pk)
return render(request, 'projects/single-project.html',
{'project': projectObj})
admin.py:
from django.contrib import admin
from .models import Project, Review, Tag
admin.site.register(Project)
admin.site.register(Review)
admin.site.register(Tag)
the files and folders
有人知道我做错了什么吗?
当您使用小写“p”在上下文处理器中传递项目时,将其设为
{% for project in projects %}
在您的模板中。
我正在尝试在我的 html 模板中创建一个 table ,
但是当我写 <td>{{project.title}}</td>
它不起作用!
实际上当我使用 {{x}} 我没有输出!
我真的不知道我的模板里面有什么问题...
html 模板:
{% extends 'main.html' %}
{% block content %}
<h1>Projects</h1>
<table>
<tr>
<th>ID</th>
<th>Project</th>
<th>Votes</th>
<th>Ratio</th>
<th></th>
</tr>
{% for project in Projects %}
<tr>
<td>{{project.id}}</td>
<td>{{project.title}}</td>
<td>{{project.vote_total}}</td>
<td>{{project.vote_ratio}}</td>
<td>{{project.created}}</td>
<td><a href="{% url 'project' project.id %}">View</a></td>
</tr>
{% endfor %}
</table>
{% endblock content %}
models.py:
from django.db import models
import uuid
from django.db.models.deletion import CASCADE
# Create your models here.
class Project(models.Model):
title = models.CharField(max_length=200)
descripeion = models.TextField(null=True, blank=True)
demo_link = models.CharField(max_length=1000, null=True, blank=True)
source_link = models.CharField(max_length=1000, null=True, blank=True)
tags = models.ManyToManyField('Tag', blank=True)
vote_total = models.IntegerField(default=0, null=True, blank=True)
vote_ratio = models.IntegerField(default=0, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.title
class Review(models.Model):
VOTE_TYPE = (
('up', 'Up Vote'),
('down', 'Down Vote')
)
# owner =
project = models.ForeignKey(Project, on_delete=CASCADE)
body = models.TextField(null=True, blank=True)
value = models.CharField(max_length=200, choices=VOTE_TYPE)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.value
class Tag(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self) -> str:
return self.name
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Project
projectsList = [
{
'id':'1',
'title':'Ecommerce Website',
'description':'Fully functional ecommerce website'
},
{
'id':'2',
'title':'Portfolio Website',
'description':'This was a project where i built out my
portfolio'
},
{
'id':'3',
'title':'Social network',
'description':'owesome open source project i am still
working'
},
]
def projects(request):
projects = Project.objects.all()
context = {'projects': projects}
return render(request, 'projects/projects.html',context)
def project(request, pk):
projectObj = Project.objects.get(id=pk)
return render(request, 'projects/single-project.html',
{'project': projectObj})
admin.py:
from django.contrib import admin
from .models import Project, Review, Tag
admin.site.register(Project)
admin.site.register(Review)
admin.site.register(Tag)
the files and folders
有人知道我做错了什么吗?
当您使用小写“p”在上下文处理器中传递项目时,将其设为
{% for project in projects %}
在您的模板中。