Здесь не нужны ни флаг, ни отдельная функция:
# use a with block to ensure the file will be properly closed
with open("accounts.txt") as file:
# files are their own iterators, no need to read the
# whole file in memory
for line in file:
# get rid of newlines / trailing whitespaces etc
loginfo = line.strip().split()
if a==loginfo[0] and b==loginfo[1]:
return render(request, 'login.html')
# if a match has been found, we'll never get here,
# so if we get here no match has been found...
return render(request, 'index.html')
Теперь я должен сказать, что хранить данные для входа в текстовый файл - наихудшая идея, особенно когда Django - полная, безопасная, работающая и очень простая в использовании система аутентификации / пользователя.