mercredi 24 octobre 2018

Python gives error because the data is not there yet?

I wrote this code:

from __future__ import print_function
import requests
from .forms import reisplannerForm
import xmltodict
import json

beginstation = ''
eindstation = ''

def createForm(request):
    global beginstation
    global eindstation

    if request.method == 'POST':
        form = reisplannerForm(request.POST)
        if form.is_valid():
            beginstation = form.cleaned_data['beginstation']
            eindstation = form.cleaned_data['eindstation']

    print(beginstation, eindstation)
    form = reisplannerForm()

    return form

def apiLinkGenerator():
    auth_details = ('email@gmail.com', '-password')
    api_url = 'http://webservices.ns.nl/ns-api-treinplanner?fromStation={}&toStation={}&departure=true'.format(
        beginstation, eindstation)
    response = requests.get(api_url, auth=auth_details)
    vertrekXML = xmltodict.parse(response.text)
    nsDump = json.dumps(vertrekXML)
    jsonLoads = json.loads(nsDump)
    jsonData = jsonLoads['ReisMogelijkheden']['ReisMogelijkheid']

    return jsonData

def main():
    data = apiLinkGenerator()

    print('Dit zijn de vertrekkende treinen naar {}: '.format(eindstation))
    for data in data:
        # Reis informatie
        vertrekTijd = data['GeplandeVertrekTijd']
        actueleReisTijd = data['ActueleReisTijd']
        status = data['Status']
        aantalOverstappen = data['AantalOverstappen']

        # Reis Deel
        reisDeel = data['ReisDeel']
        vervoerder = reisDeel[0]['Vervoerder']
        vervoerType = reisDeel[0]['VervoerType']
        reisStops = []

        print('Om {} vertrekt er een trein naar {} met de vervoerder {} {}'.format(vertrekTijd[11:16], eindstation, vervoerder, vervoerType))
        print('De actuele reistijd naar {} is {}, de status is {}. Overstappen: {}'.format(eindstation, actueleReisTijd, status, aantalOverstappen))

        for stops in reisDeel[0]['ReisStop']:
            stopNaam = stops['Naam']
            reisStops.append(stopNaam)

        print(*reisStops, sep=', ')

        return vertrekTijd, actueleReisTijd, status, aantalOverstappen, reisDeel, vervoerder, vervoerType, reisStops


vertrekTijd, actueleReisTijd, status, aantalOverstappen, reisDeel, vervoerder, vervoerType, reisStops = main()

it works but I added the

vertrekTijd, actueleReisTijd, status, aantalOverstappen, reisDeel, vervoerder, vervoerType, reisStops = main()

to access the variables in django so I can output those, I don't know if that's the correct way to do it but I came up with it. But the code does not work anymore due to it having no inputs so it can't assign these variables to any values yet? Because the error says it can't load the json and that is because I have not entered anything in my input fields yet but I can't access my webpage anymore due to this error.

I hope you can understand what I mean...

I just need all the data in the function main() I return all the variables I need I assign the returns to variables I want to use these variables in my django project.

from django.shortcuts import render
from .reisplanner import *

def home(request):
    return render(request, 'home.html', {})

def reisplanner(request):
    form = createForm(request)
    return render(request, 'reisplanner.html', {'form': form, 'apiData': main, 'test': vertrekTijd})




Aucun commentaire:

Enregistrer un commentaire