【发布时间】:2021-07-31 18:55:04
【问题描述】:
我正在尝试在我的 html 模板中创建一个表格,
但是当我写<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)
有人知道我在这里做错了什么吗?
【问题讨论】:
-
你能分享一下你渲染这个模板的视图吗?
-
正如 Willem 所说,我们需要查看视图,以便我们可以看到您在模板上下文中提供的内容。也许
Projects不在上下文中。 -
@markwalker_ 请再次查看我的问题,我编辑了它
标签: django django-models html-table django-templates