我有一个django网站,其中包含一个包含许多服务器详细信息的表。 在表格的其中一列中有" ILO"服务器的IP。
我希望网站检查" ILO" IP在http请求中工作,如果是,则应显示链接,否则显示文本。
因此,在结果中,将是具有http链接到ILO IP且有些没有。
的服务器在视图的主要类中,我创建了一个名为" checkUrlAvailable"的函数。我尝试在我的index.html中使用,并检查if条件,如果我得到真或假..
出于某种原因,我得到了错误 -
无法解析余数:'(server.IP)'来自✓ checkUrlAvailable(server.IP)'
有谁知道为什么?
的index.html -
<tr>
<th><center> #</center></th>
<th width="100%"><center> Server Name </center></th>
<th><center> Owner </center></th>
<th><center> Project </center></th>
<th width="100%"><center> Description </center></th>
<th width="100%"><center> IP Address </center></th>
<th width="100%"><center> ILO </center></th>
<th><center> Rack </center></th>
<th><center> Status </center></th>
<th><center> Actions {{ response }} </center></th>
</tr>
</thead>
<tbody>
{% for server in posts %}
<tr>
<div class ="server">
<td></td>
<td style='white-space: nowrap'><center>{{ server.ServerName }}</center></td>
<td width="100%"><center>{{ server.Owner }}</center></td>
<td style='white-space: nowrap'><center>{{ server.Project }}</center></td>
<td style='white-space: nowrap'><center>{{ server.Description }}</center></td>
<td width="100%"><center>{{ server.IP }}</center></td>
<td style='white-space: nowrap'><center>
{% if checkUrlAvailable(server.ILO) is True %}
<a href="//{{ server.ServerName }}.ilo.lab.radcom.co.il"> http://{{ server.ServerName }}.ilo.lab.radcom.co.il </a>
{% else %}
http://{{ server.ServerName }}.ilo.lab.radcom.co.il
{% endif %}
<td width="100%"><center>{{ server.Rack }}</center></td>
<td width="100%"><h4><span class="badge badge-success">Online</span></h4></td></center>
views.py -
class HomeView(TemplateView):
template_name = 'serverlist.html'
def checkUrlAvailable(url):
resp = requests.head(url)
if resp.status_code == 200:
return True
else:
return False
def get(self, request):
form = HomeForm()
query = request.GET.get("q")
posts = serverlist.objects.all()
forms = {}
if query:
posts = serverlist.objects.filter(Q(ServerName__icontains=query) | Q(Owner__icontains=query) | Q(Project__icontains=query) | Q(Description__icontains=query) | Q(IP__icontains=query) | Q(ILO__icontains=query) | Q(Rack__icontains=query))
else:
posts = serverlist.objects.all()
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = {'form' : form,'forms': forms, 'posts' : posts}
return render(request, self.template_name,args)
def post(self,request):
form = HomeForm(request.POST)
posts = serverlist.objects.all()
forms = {}
if form.is_valid(): # Checks if validation of the forms passed
post = form.save(commit=False)
post.save()
messages.success(request,'{0} has been added successfully!'.format(post.ServerName))
return redirect('serverlist')
messages.error(request,'Servername is required, please refresh the page and try again.')
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = {'form' : form, 'forms': forms, 'posts' : posts}
return render(request, self.template_name,args)
答案 0 :(得分:1)
你不能在django模板中调用函数。您需要定义自定义template tag,或者可以在checkUrlAvailable
模型中移动serverlist
:
class serverlist(models.Model):
...
def checkUrlAvailable(self):
resp = requests.head(self.ILO)
if resp.status_code == 200:
return True
else:
return False
并在模板中使用它:
{% if server.checkUrlAvailable %}