Make sure the path to the Django directory

Over the past two days, I have been struggling with the following question:

Given the absolute path (string) to the directory (inside my file system or server, it does not matter), determine whether this directory contains a valid Django project.

Firstly, I thought about searching for manage.py under it, but what if any user omitted or renamed this file?

Secondly, I thought about finding the settings module, but I want the root of the project, what if settings has level 2 or more?

Thirdly, I thought about finding the (standard) name BASE_DIR inside settings , but what if the user did not define it or rename it?

Is there a way to correctly identify a directory as a valid Django project? Did I miss something?

+5
source share
2 answers

One way you can try is to search / read the.py files in a directory and match the regular expression pattern describing the distinctive django main function and package names. It may be profitable, but ... eh

0
source

Like bruno desthuilliers mentioned in the comments, there is no failover solution.

But it depends on what you need it for. I mean how strict he is (or how good?). I used PROJECT_DIR several times instead of BASE_DIR . As for the settings, I saw one settings.py file and several settings files in the settings directory.

The main problem will be Django, which does not have such strict rules for naming files / modules. You can pretty much name your files no matter how you put the pieces together correctly.

If I'm not mistaken, the only tough Django rule is the models.py file. A Django application must have a file called models.py . But your Django application does not have to contain any application.

If you only need a good enough solution, I would say that manage.py is a good candidate. You can double check and open the file and see if there is django import there. If there is no manage.py , check for requirements.txt and see if Django is included in it. If there is no requirements.txt , check the directory with the requirements name for the text files inside.

Something you could do to find some templates is to browse the repositories tagged with the "django" tag on GitHub . Perhaps use their APIs and clone all repositories (they will only list 2000 repositories 2000) to your local computer or server. Cloning some rails , javascript , etc. Repos also and write down your code step by step. Just do a manage.py search if the result is satisfactory that it is. If not, add a couple more rules and test against the cloned repositories until you find a good enough solution for your problem.

0
source

Source: https://habr.com/ru/post/1275450/


All Articles