1

I have a model that contains domain names which I need to ping. I was able to create the view but I couldn't figure out how to output it on the template.

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)

the model

class Table(models.Model):
  page = models.URLField(verbose_name=None)

and this is how I call it on the template

{% if online %}
  <i class="small material-icons green-text">check_circle</i>
{% else %}
  <i class="small material-icons red-text">close</i>
{% endif %}

Can anyone guide me? It's returning data_upload() missing 1 required positional argument: 'page'

4

1 Answer 1

2

You've got several problems in your code making it incorrect:

  • domain = Table.objects.filter(page) will fail. You should do domain = Table.objects.filter(page=page)
  • You are not actually pinging a domain as domain is just a string in your code subprocess.check_call(['ping', '-c', '1', "domain"])
  • You are not using a context: if you want to access it in the template, you need to pass it to the render call: return render(request, 'home.html', context)

Even more, Django's filter function returns a queryset object, not a single entity.

My suggestion is to fix your code as follows:

views.py

from django.shortcuts import get_object_or_404


def pingDomain(request, page):
  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 = {
      'online': host_online
  }
  return render(request, 'home.html', context) 

And then in your template you can simply access it via online key:

{{online}}
14
  • There's no output on the template. I also iterated {% for o in online %} {{o.online}} {% endfor %} but still did not print any code on the template. Oct 6, 2019 at 15:52
  • Because online is not iterable, you can't iterate over it. It is just a status of one single page Oct 6, 2019 at 15:55
  • I currently have 6 items on the page for model. and even if I don't iterate on it, it won't show any output. Oct 6, 2019 at 15:56
  • What is a page in your code? Does it stand for domain (web page) or for page of objects? Provide an example of your page argument Oct 6, 2019 at 15:58
  • 1
    In your template you've got a list of domains. But the discussed view simply handles exactly one domain at a time. How do you request this view from the template? It seems to me that the view that renders that list and the view to ping the page are different views. If so you should ping the domains in the very same view that renders the list Oct 6, 2019 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.