我有一个管理命令,可以打印出功能输出。但是执行后会出现错误。
什么是错误以及如何解决?
from django.core.management.base import BaseCommand
from Destinations.models import *
from LCR.views import *
class Command(BaseCommand):
help = 'Generates a LCR when executed '
def handle(self,*args, **kwargs):
LCR(self)
(VE)C:\ Users \ Bitswits 3 \ Desktop \ LCRProject \ LeastCostRouting> python manage.py my_dest_commands {'48':['Tata','0.531','Tata',' 0.531”,“塔塔”,“ 0.531”,“塔塔”,“ 0.531”,“塔塔”,“ 0.531”,“塔塔”,“ 0.531”],“ 23”:['塔塔”,“ 4.150”,“塔塔”,“ 4.150”,“塔塔”,“ 4.150','Tata','4.150','Tata','4.150','Tata','4.150','PTCL','0.888','PTCL','0.888','PTCL','0.888' ,“ PTCL”,“ 0.888”, 'PTCL','0.888','PTCL','0.888']}
回溯(最近一次通话最近):文件“ manage.py”,第22行,在 execute_from_command_line(sys.argv)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ core \ management__init __。py”, 第364行,在execute_from_command_line中 utility.execute()文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ core \ management__init __。py”, 第356行,执行 self.fetch_command(子命令).run_from_argv(self.argv)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ core \ management \ base.py”, 第283行,在run_from_argv中 self.execute(* args,** cmd_options)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ core \ management \ base.py”, 执行中的第330行 输出= self.handle(* args,** options)文件“ C:\ Users \ Bitswits 3 \ Desktop \ LCRProject \ LeastCostRouting \ Destinations \ management \ commands \ my_dest_commands.py”, 第14行,在句柄中 在LCR中打印LCR(自己)文件“ C:\ Users \ Bitswits 3 \ Desktop \ LCRProject \ LeastCostRouting \ LCR \ views.py”,行269 返回渲染(请求,模板,{“ ratelist”:费率表,“ emailadd”:emailadd})文件 “ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ shortcuts.py”, 第30行,在渲染中 内容= loader.render_to_string(模板名称,上下文,请求,使用=使用)文件 “ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ template \ loader.py”, 第68行,在render_to_string中 返回template.render(上下文,请求)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ template \ backends \ django.py”, 第66行,在渲染中 返回self.template.render(context)文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ template \ base.py”, 渲染中的第205行 使用context.bind_template(self):文件“ c:\ python27 \ Lib \ contextlib.py”,第17行,输入 返回self.gen.next()文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ template \ context.py”, 第263行,在bind_template中 updates.update(processor(self.request))文件“ C:\ Users \ BITSWI〜1 \ Desktop \ LCRPRO〜1 \ VE \ lib \ site-packages \ django \ template \ context_processors.py”, 第43行,在调试中 如果settings.INTERNAL_IPS中的settings.DEBUG和request.META.get('REMOTE_ADDR')无效 属性“ META”
def LCR(request):
template = "LCR\LCRGen.html"
emailadd = Email.objects.all()
dest = Destination.objects.values_list('dest_num', flat=True)
ratelist = {}
csv_file = { }
data_set = { }
io_string = { }
vendor = RateFile.objects.values_list()
v_count = vendor.count()
for v_id, v_name, v_file in vendor:
vendor_name = str(v_name)
#converted into a string so that i may be used to reference for file creation with the vendor name. like this (csv_fileTata)
vendornames = str(v_name)
#converted into a string to use as an empty dict var name
vendornames = { }
for desNum in dest:
desNum = str(desNum)
for countvar in range(v_count):
csv_file[vendor_name] = RateFile.objects.get(id=v_id).ven_file
data_set[vendor_name] = csv_file[vendor_name].read().decode("UTF-8")
io_string[vendor_name] = io.StringIO(data_set[vendor_name])
next(io_string[vendor_name])
for column in csv.reader(io_string[vendor_name], delimiter=str(u",")):
#creates a dictionary with the name of the vendor,the number as key and rate as value
vendornames[column[0]] = column[1]
# csvfile.close()
for venNum, venValue in vendornames.items():
venlen = len(venNum)
deslen = len(desNum)
# csvfile = open('csvfile.csv','w')
if venlen >= deslen:
if desNum[:-1]==venNum[:-1] and desNum[:-2]==venNum[:-2] and desNum[:-3] == venNum[:-3]:
# print ('====================')
# print (venNum)
# print (desNum)
# print "Works well 1"
# print ('====================')
# ratelist[desNum] = [vendor_name, venValue]
# ratelist[desNum].append(venValue)
# rates = dict((desNum, tuple(vendor_name))
# for desNum, vendor_name in ratelist.items())
# thewriter.writerow
# ({'Destinations':[desNum], 'Vendors':[vendor_name], 'Rates':[venValue] })
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
if vendor_name and venValue in desNum:
print 'stuff'
else:
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
elif desNum[:-1] == venNum[:-2] and desNum[:-2] == venNum[:-3] and desNum[:-3] == venNum[:-4]:
# print ('====================')
# print (venNum)
# print (desNum)
# print (venValue)
# print "Works well 2"
# print ('====================')
# ratelist[desNum].append(vendor_name)
# rates = dict((desNum, tuple(venValue)) for desNum, vendor_name in ratelist.iteritems())
# thewriter.writerow({'Destinations':[desNum], 'Vendors':[vendor_name], 'Rates':[venValue]})
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
if vendor_name and venValue in desNum:
print 'stuff'
else:
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
elif desNum[:-1] == desNum[:-3] and desNum[:-2] == venNum[:-4] and desNum[:-3] == venNum[:-5]:
# ratelist[desNum].append(vendor_name)
# rates = dict((desNum, tuple(venValue)) for desNum, vendor_name in ratelist.iteritems())
# thewriter.writerow
# ({'Destinations':[desNum], 'Vendors':[vendor_name], 'Rates':[venValue] })
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
if vendor_name and venValue in desNum:
print "STUFF"
else:
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
# print ('====================')
# print (venNum)
# print (desNum)
# print (venValue)
# print "Works well3"
# print ('====================')
elif desNum[:-1] == venNum[:-4] and desNum[:-2] == venNum[:-5]:
# ratelist[desNum].append(vendor_name)
# rates = dict((desNum, tuple(venValue)) for desNum, vendor_name in ratelist.iteritems())
# thewriter.writerow
# ({'Destinations':[desNum], 'Vendors':[vendor_name], 'Rates':[venValue] })
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
if vendor_name and venValue in desNum:
print "FOUND"
else:
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
elif desNum[:-1] == venNum[:-5] and desNum[:-2] == venNum[:-6]:
# ratelist[desNum].append(vendor_name)
# rates = dict((desNum, tuple(venValue))
# for desNum, vendor_name in ratelist.iteritems())
# print rates
# thewriter.writerow
# ({'Destinations':[desNum], 'Vendors':[vendor_name], 'Rates':[venValue] })
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
if vendor_name and venValue in desNum:
print 'STUFF'
else:
ratelist.setdefault(
desNum, []).append(vendor_name)
ratelist.setdefault(
desNum, []).append(venValue)
# print ('====================')
# print (venNum)
# print (desNum)
# print (venValue)
# print "Works well5"
# print ('====================')
else:
# thewriter.writerow({'Destinations': [desNum], 'Vendors': [
# vendor_name], 'Rates': [venValue]})
pass
# thewriter.writerow(
# {'Destinations':desNum, 'Vendors':vendor_name, 'Rates':venValue})
# csvfile.close()
csvfile = open('csvfile.csv','w')
headers = ['Destinations', 'Vendors', 'Rates']
thewriter = csv.DictWriter(csvfile, fieldnames=headers)
thewriter.writeheader()
thewriter.writerow({'Destinations':venNum, 'Vendors':[vendor_name], 'Rates':[venValue] })
print ( ratelist )
return render (request, template, {"ratelist": ratelist, "emailadd": emailadd } )
答案 0 :(得分:1)
在您的Command中,将命令实例本身传递给函数。
/users/$(request.auth.uid)
但是您的LCR()函数似乎是一个视图,有点像这样,并且期望最后一个render语句有一个request实例(HttpRequest)。我猜这就是为什么您会收到此错误。
if (sapply(df[,col],is.factor)) {
据我所知,由于请求实例未对LCR逻辑使用任何内容,因此应将其从View函数封装到单独的函数中,以便可以从两个位置调用它。
例如:
在您的> LeastCostRouting \ LCR \ views.py 例如
class Command(BaseCommand):
help = 'Generates a LCR when executed '
def handle(self,*args, **kwargs):
LCR(self) # <<
然后只需在您的LCR方法中调用它即可,
def LCR(request): template = "LCR\LCRGen.html" # .... return render (request, template, {"ratelist": ratelist, "emailadd": emailadd } )
在您的命令方法中,例如:
def get_LCR(): # take everything from the original method starting: emailadd = Email.objects.all() # --- # the other code # till: print(ratelist) return {"ratelist": ratelist, "emailadd": emailadd }