在我的应用程序文件夹中,我有views.py
和bot.py
。
这个bot.py公司是我项目的主脑,我的所有功能都是“做”主要事情(后端)。我把它们导入Django使用。现在我需要把数据从django发送到bot.py公司. 怎么做?我有:
你知道吗视图.py地址:
bot = Instagram()
class InstabotFormView(AjaxFormMixin, FormView):
form_class = LoginInstagramForm
template_name = 'instabot.html'
success_url = 'runinstabot.html'
def form_invalid(self, form):
response = super(InstabotFormView, self).form_invalid(form)
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
else:
return response
def form_valid(self, form):
response = super(InstabotFormView, self).form_valid(form)
login = form.cleaned_data.get('login')
password = form.cleaned_data.get('password')
tagi = form.cleaned_data.get('tags')
tags = []
tagi = tagi.split(',')
tags.extend(tagi)
print(tags)
if self.request.is_ajax():
print('It is AJAX')
bot.login(login,password)
bot.search(tags)
print(form.cleaned_data)
data = {
'message': "Succesfully opened Selenium."
}
return JsonResponse(data)
else:
return response
你知道吗bot.py公司地址:
class Instagram(object):
...
def search(self,tags):
time.sleep(1)
search = self.driver.find_element_by_xpath('/html/body/span/section/nav/div[2]/div/div/div[2]/input')
search.clear()
search.send_keys(tags[self.nextTag], Keys.ENTER)
time.sleep(2)
self.driver.find_element_by_xpath(
'/html/body/span/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]/div').click()
def changeTag(self):
#if a list gets to the end reset it
self.nextTag += 1
tagsNum = len(Instagram.tags)
if self.nextTag == tagsNum:
self.nextTag = 0
else:
self.tags[0 + self.nextTag]
return self.nextTag
在我运行脚本后,它写道:
AttributeError: 'Instagram' object has no attribute 'tags'
我知道Instagram.tags
不存在,但是如何在我的bot.py
中使用来自Django的数据呢?如何从Django类中放置而不是Instagram.tags
,tags=[]
?还是有更好的解决办法?(可能在Django中写入changeTag(),并将数据返回给机器人?) 你知道吗
我试过在里面导入Django类bot.py公司但不起作用
这与Django无关。你只是在处理Python类。类为它们的实例定义属性和方法。因此,如果您在另一个对象(a
InstabotFormView
)中初始化一个Instagram
实例,您只需将值/对象赋给它的属性即可。你知道吗现在,如果您将
tags = []
属性添加到Instagram
(用空lost初始化它),那么在Django类中,您可以将bot.tags
设置到标记中,并在Intagram
方法中使用它。你知道吗相关问题 更多 >
编程相关推荐