【发布时间】:2019-10-06 15:12:51
【问题描述】:
我有一个模型,其中包含我需要 ping 的域名。我能够创建视图,但不知道如何在模板上输出它。
def index(request, page):
template = "home.html"
if request.method == 'POST':
csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request, 'Please upload a .csv file.')
data_set = csv_file.read().decode('ISO-8859-1')
io_string = io.StringIO(data_set)
next(io_string)
for column in csv.reader(io_string, delimiter=','):
_, created = Table.objects.update_or_create(
page=column[0],
keyword=column[1],
interval=column[2],
email=column[3],
notes=column[4],
billing=column[5],
)
page_object = get_object_or_404(Table, page=page)
try:
subprocess.check_call(['ping', '-c', '1', page_object.page])
except subprocess.CalledProcessError:
host_online = False
else:
host_online = True
context = {
'tables': Table.objects.all(),
'online': host_online,
'page': page
}
return render(request, template, context)
模型
class Table(models.Model):
page = models.URLField(verbose_name=None)
这就是我在模板上的称呼
{% if online %}
<i class="small material-icons green-text">check_circle</i>
{% else %}
<i class="small material-icons red-text">close</i>
{% endif %}
谁能指导我?
它正在返回data_upload() missing 1 required positional argument: 'page'
【问题讨论】:
-
您没有将
context传递给模板。 -
@WillemVanOnsem 我刚刚做了,但是当我循环浏览它时,桌子不见了。
-
那么
t是什么? -
来自模型的迭代
标签: django django-models django-templates django-views