将Django视图转换为Python-fi

2024-09-29 21:41:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对django的看法如下:

from xmlsoccer import XmlSoccer
from straightred.models import StraightredFixtureLive
    @csrf_exempt
    def updatefixtureslive(request, soccerseason, league):

        if request.user.is_authenticated():

            xmlsoccer = XmlSoccer(api_key='XYZ123ABC789', use_demo=False)
            fixtureslive = xmlsoccer.call_api(method='GetLiveScoreByLeague',
                                       league='English Premier League')

        count = 0

            for fixturelive in fixtureslive:



                if 'Id' in fixturelive.keys():

                count = count + 1       
                    fixtureLiveUpdate = StraightredFixtureLive(fixtureid_id=fixturelive['Id'],
                                                           away_team_id = fixturelive['AwayTeam_Id'],
                                                           home_team_id = fixturelive['HomeTeam_Id'],
                                                           fixturedate = fixturelive['Date'],
                                                           fixturestatus = fixturelive['Time'],
                                                           fixturematchday_id = fixturelive['Round'],
                                                           spectators = fixturelive['Spectators'],
                                                           hometeamscore = fixturelive['HomeGoals'],
                                                           awayteamscore = fixturelive['AwayGoals'],
                                                           homegoaldetails = fixturelive['HomeGoalDetails'],
                                                           awaygoaldetails = fixturelive['AwayGoalDetails'],
                                                           hometeamyellowcarddetails = fixturelive['HomeTeamYellowCardDetails'],
                                                           awayteamyellowcarddetails = fixturelive['AwayTeamYellowCardDetails'],
                                                           hometeamredcarddetails = fixturelive['HomeTeamRedCardDetails'],
                                                           awayteamredcarddetails = fixturelive['AwayTeamRedCardDetails']
                                )
                    fixtureLiveUpdate.save()

            return HttpResponse("Live games have been updated." + str(count))
        else:
            return HttpResponse("You must be logged in to update teams.")

我已经删除了所有我认为只是django特有的部分,结果是:

from xmlsoccer import XmlSoccer
from straightred.models import StraightredFixtureLive

xmlsoccer = XmlSoccer(api_key='XYZ123ABC789', use_demo=False)
fixtureslive = xmlsoccer.call_api(method='GetLiveScoreByLeague',
                                   league='English Premier League')

count = 0

for fixturelive in fixtureslive:



if 'Id' in fixturelive.keys():

    count = count + 1
    fixtureLiveUpdate = StraightredFixtureLive(fixtureid_id=fixturelive['Id'],
                                                       away_team_id = fixturelive['AwayTeam_Id'],
                                                       home_team_id = fixturelive['HomeTeam_Id'],
                                                       fixturedate = fixturelive['Date'],
                                                       fixturestatus = fixturelive['Time'],
                                                       fixturematchday_id = fixturelive['Round'],
                                                       spectators = fixturelive['Spectators'],
                                                       hometeamscore = fixturelive['HomeGoals'],
                                                       awayteamscore = fixturelive['AwayGoals'],
                                                       homegoaldetails = fixturelive['HomeGoalDetails'],
                                                       awaygoaldetails = fixturelive['AwayGoalDetails'],
                                                       hometeamyellowcarddetails = fixturelive['HomeTeamYellowCardDetails'],
                                                       awayteamyellowcarddetails = fixturelive['AwayTeamYellowCardDetails'],
                                                       hometeamredcarddetails = fixturelive['HomeTeamRedCardDetails'],
                                                       awayteamredcarddetails = fixturelive['AwayTeamRedCardDetails'])
    fixtureLiveUpdate.save()

但是,我只得到以下错误:

bash: update_live.py: line 6: syntax error near unexpected token `('

我想要的是使用looking创建python文件,然后使用crontab在预定的时间运行它。你知道吗

如果有人能就此提供任何建议,我们将不胜感激。你知道吗

非常感谢,艾伦。你知道吗


Tags: infromimportapiidcountteamleague
1条回答
网友
1楼 · 发布于 2024-09-29 21:41:12

如果要创建脚本,请使用django项目启动脚本:

import os
import sys
import django

#Don't forget '/' at end in path.
PATH_PROJECT = os.path.dirname('/path/to/your/project/')
sys.path.append(PATH_PROJECT) 

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
django.setup()

from straightred.models import StraightredFixtureLive

## many operations.

为该脚本添加cronjob之后。你知道吗

相关问题 更多 >

    热门问题