python - local variable 'server_name' referenced before assignment in django view -
i creating django app stores servers in database. servers come list via third party app. app needs go through list add servers aren't in database , delete in database aren't in list. however, when try deletion part of requirements keep getting referenced before assignment
error part of code states server.objects.filter(server_name in i)
not sure if can server_name in i
way have done playing around it. have got error when doing server.objects.filter(server_name=i)
(which know should work). can help?
from django.shortcuts import render django.http import httpresponse .models import server hive.cloud.pyraxhelpers.pyraxhelpers import list_hostnames django.core.exceptions import objectdoesnotexist def server_home(request): return render(request, 'templates/index.html', {}) def server(request): server_name = server.objects.get(server_name) in list_hostnames(): print "going through list" if server.objects.filter(server_name=i).exists(): print "server exists skip" else: if server.objects.filter(server_name in i): print "add new server" server_name = server.objects.create(server_name=i) return httpresponse("added") else: print "delete" return httpresponse("deleted") return httpresponse("worked")
model
class server(models.model): server_name = models.charfield(primary_key=true, max_length=250) renewal_date = models.datefield('expiry date', null=false, default=timezone.now) deletion_date = models.datefield('deletion date', null=false, default=timezone.now) def __unicode__(self): return self.server_name
you're getting error because you're referencing server_name
variable before assignment, error says.
the problem in line:
server_name = server.objects.get(server_name)
what should second server_name
variable contain? python doesn't know.
it looks want add argument call, def server(request, server_name)
. make sure change urls.py.
Comments
Post a Comment