I am working on a website. For this website I have to create a table which shows the time of the meeting and the participants of it. I am writing the code of site using Django. For the table I have a data on a .json file. When I run the server locally it works fine and when I push it to heroku it raises FileNotFoundError. I found this question which faces with the similar problem but the answer shows how to log something, but I need .json file for data. My django app directory:
66 admin.py
100 apps.py
<DIR> migrations
60 models.py
3,332 schedule_of_teams.json
<DIR> static
<DIR> templates
63 tests.py
333 urls.py
807 views.py
So I have a view function in views.py file:
def schedule_of_teams(request):
with open('./schedule_of_teams.json',encoding='utf-8') as f:
context = json.load(f)
context['title'] = 'Yarış cədvəli'
return render(request,'schedule_of_teams.html',context)
And open context manager raises the error:
FileNotFoundError at /schedule_of_teams/
[Errno 2] No such file or directory './schedule_of_teams.json/'
So why is this happening? Why it works fine when I run python manage.py runserver with localhost but when I push to heroku it raises this exception? Is heroku ignores .json files? By the way, I do not have *.json on my .gitignore file it commits the file.
What I have tried:
I tried to change the directory of the file like moving it to a folder data/ or firstly instead of open('./schedule_of_teams.json) I wrote open('schedule_of_teams.json') but that didn't work either.
I am literally so confused I asked this question in Python Discord Server but got no respond. One of the solutions is creating a database but I do not want to do it, since this data is just a list which has 16 dictionaries and each has 4 keys.
Related
I am doing an assignment to integrate a python script with HTML templates, and while the IDE is giving me no errors, when I try to actually run the website it gives me an "internal server error" and in the debug menu it says either that it doesn't recognize the Flask command "render_template" or doesn't recognize the HTML file it's supposed to render. The HTML file is stored both in the same folder as the python file and a copy is stored in a seperate "templates" folder located in the same folder as the python file. I really have no idea what's going on, the teacher has been no help.
I tried renaming the files, spell checked the imports repeated, checked all the commands, made copies of the python and Html files to see if the directorry was the problem. I really am not sure what else to try
...from flask import Flask
...from flask import render_template
...from flask import request
...from flask import flash
...import datetime
...app = Flask(__name__)
```#app.route('/')
...#the home page
...def home():
... return render_template('home_page.html')
```#app.route('/clock/')
...#the clock site
...def time_site():
... return date_and_time()
...def date_and_time():
... cur_time = str(datetime.now())
...return render_template('clock.html', cur_time)
```if __name__ == "__main__":
...app.run()
The following python code successfully fills out a pdf form:
import pypdftk
data_dict = {key:value pairs}
PDF_PATH = 'form.pdf' #form to be filled out in same folder as the file executing this code
out_file = 'out_file.pdf' #completed pdf file
generated_pdf = pypdftk.fill_form(
pdf_path = PDF_PATH,
datas = data_dict,
out_file = out_file,
)
However, the same code used in my django project results in the following error message:
Error: Unable to find file.
Error: Failed to open PDF file:
form.pdf
Errors encountered. No output created.
Done. Input errors, so no output created.
... REMAINDER OF TRACEBACK EXCLUDED FOR BREVITY IF YOU WANT TO SEE IT I'LL POST...
raise subprocess.CalledProcessError(retcode, cmd, output=output) output=output) df fill_form C:\Users\Home\AppData\Local\Temp\tmpbqq__7c4 output out_file.pdf flatten
subprocess.CalledProcessError: Command 'pdftk l_a_r.pdf fill_form C:\Users\Home\AppData\Local\Temp\tmpbqq_0 87495_7c4 output out_file.pdf flatten'
returned non-zero exit status 1.
pypdftk is installed in the virtual environment the project is running in.
The pdftk server is added as a windows path variable.
In the above example, and every other time this has happened the temp file referenced at the end of the error message contains all of the expected data in XML.
I've tried the following combinations of code to try to make this work:
Running the exact above code within a view function, with the pdf form to be filled in the same folder as the views.py file:
import pypdftk
def filler_view(request):
form = MyForm()
if request.method =='POST':
#code to successfully populate dictionary data_dict with form data
PDF_PATH = 'form.pdf' #form to be filled out in same folder as the file executing this code
out_file = 'out_file.pdf #completed pdf file
generated_pdf = pypdftk.fill_form(
pdf_path = PDF_PATH,
datas = data_dict,
out_file = out_file,
)
return render(request, 'success.html')
Storing the code and file in a folder and importing to call the relevant function within the view:
-appFolder
-pfd_filler_folder
-form.pdf
-form_filler.py
-views.py
views.py
from appFolder.pdf_filler_folder import form_filler as f
def filler_view(request):
form = MyForm()
if request.method =='POST':
#code to successfully populate dictionary data_dict with form data
f.fill_form(data_dict, 'output.pdf')
form_filler.py:
import pypdftk
def fill_form(data_dict, out_file):
PDF_PATH = 'form.pdf'
generated_pdf = pypdftk.fill_form(
pdf_path = PDF_PATH,
datas = data_dict,
out_file = out_file,
)
Running both of the above with the full path from c:\... of the form.pdf file.
I've also verified that I can successfully fill a form with the executing .py file and the form.pdf file in same folder on two storage drives and from within the django project itself, when not being executed by the django project. pdftk finds the forms.py with no problems at all in this circumstance.
I believe that the file not found error message is key, as it seems to refer to the pdf form I'm trying to fill out. I've spent from 1500 till 1800 researching this, and I haven't managed to get it to work, although I am lead to believe that my error message indicates a missing parameter in the cl execution command. I'm not sure what this would be, as all parameters seem present and correct.
Interestingly enough, a friend of mine is experiencing the same error message just in windows. I'm aware that pdftk can sometimes be touchy in windows, and I think there's probably a nuance I'm missing here.
The outcome I'd like is to fill out a pdf form from within my django project, with data obtained from a form through a post request.
I'd welcome either someone enlightening me as to why pdftk is struggling to either see or use the form file whilst being used from within my django project and pointing me in the right direction
I'm aware that there are alternatives to using pdftk, but pdftk is the simplest, and honestly pypdftk is the only library I've found to reliably work with python to fill out pdf forms so far in Windows. I don't want to go down the route of generating my own replica form and populating it with data, but I'm aware that that is also an option.
Question answered just now on Reddit:
When in Django, it is either wsgi.py or manage.py which is ultimately responsible for what goes on. On that basis, placing the form.pdf file in the same folder as wsgy.py solved the problem and the code now runs as intended, with an unbound form POSTing data back to a view, and a pdf form being filled out and a duplicate saved with said data. Hope that helps anyone else who comes up against this!
Strange error, think I must be missing something obvious. I've created a services.py file in my app which will populate my database when the file is run. So it isn't run every time the server boots/queries the file I've added:
if __name__ == '__main__':
import ...
from models import Authority, Rating
<logic to populate database>
However, when I run the file, Django is throwing the exception:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Am I running this file from the wrong place? Would it be better suited to my models file? If so, how do I stop it running every time the server boots?
I setup some RADIUS backend to allow AD authentication via the 'admin' of django. Alltough i got a problem with some dictionaries, i really don't know what i'm doing wrong. This is the error i got:
IOError at /admin/
Errno 2] No such file or directory: '/home/pl/dictionary.compat'
I installed pyrad, so it should be there and i've got a 'dic't file in the following style (/home/pl/dict):
#
# Version $Id: dictionary,v 1.1.1.1 2002/10/11 12:25:39 wichert Exp $
.....
$INCLUDE dictionary.compat # compability issues
$INCLUDE dictionary.acc
$INCLUDE dictionary.ascend
$INCLUDE dictionary.bay
....
The code i use in the RADIUS backend:
srv = Client(server=settings.RADIUS_SERVER,
secret=settings.RADIUS_SECRET,
dict=Dictionary("/home/pl/dict"))
Any ideas?
The $INCLUDE directive in the configuration files is intended to add definitions from another dictionary file. Unless the extra dictionary files are found, the dictionary object cannot be created.
My advice is:
- if you don't have the extra dictionary files: comment out/remove the $INCLUDE lines
- if you have the extra dictionary files: copy them to the right location
I am trying to set up Google App Engine unit testing for my web application. I downloaded the file from here.
I followed the instructions in the readmen by copying the directory gaeunit into the directory with the rest of my apps and registering 'gaeunit' in settings.py. This didn't seem sufficient to actually get things going. I also stuck url('^test(.*)', include('gaeunit.urls')) into my urls.py file.
When I go to the url http://localhost:8000/test, I get the following error:
[Errno 2] No such file or directory: '../../gaeunit/test'
Any suggestions? I'm not sure what I've done wrong. Thanks!
You can copy url but in your urls.py include
(r'^test', include('gaeunit.urls')),
Also you will have to change test path in gaeunit.py
_LOCAL_DJANGO_TEST_DIR = 'test'
And in your main web test files under test folder include following line for django test setup
self.application = django.core.handlers.wsgi.WSGIHandler()
According to the instructions on the main page you should add the URL to app.yaml, not urls.py.
- url: /test.*
script: gaeunit.py