模型
class URL_monitor(models.Model):
url = models.TextField(unique=True, verbose_name='URL')
interval = models.PositiveIntegerField(verbose_name='Interval (in seconds)')
def __str__(self):
return self.url
def get_absolute_url(self):
return reverse('index')
查看
def IndexView(request):
urls = URL_monitor.objects.all()
response = requests.get(urls)
status = response.status_code
context = {
'urls' : urls,
'status' : status,
}
return render(request, 'index.html', context)
模板
{% if urls %}
{% for url in urls %}
<p>Status: {{ status }} {{ url.url }} <a href="{% url 'delete_url' url.pk %}">Delete</a></p>
{% endfor %}
{% else %}
<p>No URL's added, <a href="{% url 'add_url' %}">add some</a></p>
{% endif %}
我只想从模型中获取一个url并为其获取状态代码。我在数据库中的url字段具有一个值:http://google.com/