Chainging directory of flask project - everything fails - python

Hi I had followed project organisation in directory ./my_project/
-venv/
-static/
-templates/
--index.html
--login.html
-requirements.txt
-main.py
-views.py
then i wanted to add test folder and reorganise all to following structure:
-venv/
-my_project/
--static/
--templates/
---index.html
---login.html
--requirements.txt
--main.py
--views.py
And when I am running main file, server runs, but if I open on a browser right address there is error 404. In log of a server I see that server recieves request, but something is wrong. I have no idea what is this, because I didn't change anything in code.

You need to make the my_project a Python package by adding an empty __init__.py file inside it.
See this answer here https://stackoverflow.com/a/448279/9379795

Related

IIS can not access newly added files in a django project

In a django project that is served by IIS (windows), i added a local file, test.py. The project ran perfectly before and still runs perfect on localhost, however IIS appears to not recognize the new test.py file. It appears IIS access to this file fails, even if the users IUSR and IIS_USRS have full access (same as for all other files in the folder).
I get below error message, and somethimes also the same but "No module named 'app.test'. Removing the "import app.test as test" in views.py solves the issue.
Suprisingly the "import app.oldFile as oldFile" works without issue.
In my views.py, i import the python scripts like this
import app.oldFile as oldFile
import app.test as test
My django project has the structure:
djangoRest
-app
--__init__.py
--views.py
--oldFile.py
--test.py
-djangoRest
--__init__.py
--settings.py
--urls.py
--wsgi.py
I found the reason, and solution.
In the earlier import file oldFile.py i use the line
import os
os.chdir(r\\some\\other\\directory\\)
This obviously changes the current working directory and as a result the other file test.py can't be found, and cannot be imported.
This mistake does not matter when testing django on the localhost, but only manifests in an issue when served by IIS.

How to tell passenger_wsgi.py to look for Django project inside another folder?

I'm trying to host a django app on cpanel but i cant find a way to tell passenger_wsgi.py to look for the django project(main file) inside another folder
My site structure is:
/home/project/
tmp/
public/
passenger_wsgi.py
abdi-group/
passenger_wsgi.py:
from abdiGroup.wsgi import application
this works fine if i move everything inside abdi-group/ to /home/project/
I tried this:
passenger_wsgi.py:
from abdi-group.abdiGroup.wsgi import application
but it can't find abdiGroup(django project name) inside abdi-group/
am i missing something?
abdiGroup is supposed to be a folder in your project directory that contains the wsgi file not abdi-group. If you're making it a subfolder add an empty __init__.py to any new folder so Django knows it is a package and can import from it.

Error with flask frozen module for my website

So I've been trying to deploy my flask website to the public, but it's been quite annoying to say the least. I'm finished with my website and it runs on the localhost. However, when I try to use the frozen-flask module, as suggested by a tutorial I saw on Medium.com, to "freeze" my html files into static files so I can have a static website (I made a personal website), I get an error in my freeze.py file. Here's the code for it:
from flask_frozen import Freezer
#main.py is the python file where I created my app
from main import app
freezer = Freezer(app)
if __name__ == '__main__':
#This is the line that appears to be giving me the error when I run the code from the terminal.
freezer.freeze()
That Medium.com tutorial only used 1 html file and no css (just a basic hello world application), so I have no clue if that would change anything, as I have several html files and a main.css file. Also, when I run "python freeze.py" in the terminal, it creates a directory in my project named "build" (as expected) but underneath the directory, it creates several other directories, named after all my html files. Underneath those directories, it has a file called "index.html" (for ALL of the directories). The files look something like this because I know that explanation wasn't too good. For some reason, this didn't happen when I initially used flask-frozen, it just created a bunch of files named after my html files, with no unnecessary extra directories.
- Name of project
- build
- name of one of my html files
- index.html
- name of one of my html files
- index.html
etc...
If anyone knows why this happens, or if I even need to frozen-flask to deploy my personal website (please, god, let there be a better approach), it would be greatly appreciated if I could know. I'm trying to use Netlify if that helps at all (seems to be much more straight forward than other platforms like Heroku).
You do not need a frozen flask to deploy website, a basic flask app will do. Just make sure to have the correct port to run on.
I recommend pretty-printed's tutorials on flask
https://www.youtube.com/watch?v=CjYKrbq8BCw&list=PLXmMXHVSvS-CoYS177-UvMAQYRfL3fBtX
For flask applications, you generally want a format of:
Application folder
- app.py
- other.py
static/
- css
- js
- img
templates/
- index.html
- other.html

Can a Python script use a Django database while not being in the same folder?

I have a python application (which we'll call app) I'd like to have a web front for. The application has many files and it's important the folder tree structure stays the way it is.
I've built a Django project and put it in a folder named "Web" in the app's folder, so now the folder tree looks like so:
[Data]
[Resources]
[Web]
[WebFront]
normal django app files
[Web]
__init__.py
settings.py
urls.py
wsgi.py
__init__.py
manage.py
main.py
Here's the code on the app's main.py:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.Web.settings")
django.setup()
This code causes an exception on the django.setup() line as (I think) django does not find the project modules: ImportError: No module named WebFront (WebFront is the name of the django app)
I suspect this is caused because django runs in the directory of python app, and therefore cannot find the folder WebFront - Which should actually be Web/WebFront
Can this be done? Or should I reverse the order and put the python app in the django app?
This is not a duplicate of the following questions as the folder nesting causes a different problem (I think)
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
Easiest way to write a Python program with access to Django database functionality
Using only the DB part of Django
You can locate your main.py script where you like. However, if it is outside of the Web folder, then you will have to add Web to the Python path, otherwise imports like import Webfront are going to fail.
import sys
sys.path.append('/path/to/Web/')
Once you have done that, you can change the DJANGO_SETTINGS_MODULE to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.settings")

Google App Engine won't recognize facebook package unless I rename it

I'm trying to intergrate Facebook Connect into an GAE app. I've got a basic folder structure like so:
/gae-root
/myapp
/templates
/etc
app.yaml
settings.py
and I tried to add the PyFacebook library like so:
/gae-root
/myapp
/templates
/etc
/facebook
/djangofb
app.yaml
settings.py
I thought this would work, but now when I try to import facebook it throws a module not found error. What's even weirder is that if I rename the directory from facebook to foo, the import now works but I'll hit errors later when I try to get the current logged in user.
Literally all I did was move the directory into my folder structure and try an import. What am I missing? Sorry if this is an easy question.
It was a problem with an extra .pth file in my site-packages directory.

Categories