Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Django name 2 folders the project name?

So in my Django stucture (default setup), my folder structure goes like this:

/mysite
    /mysite
        __init.py
        settings.py
        urls.py
        wsgi.py
    /mymodel
        ...    
    /myapp
        ...
    manage.py

So to access the settings file, wsgi.py is, by default, set to mysite/settings.py. However, Django gave the mysite name to 2 folders, one within the other. So it looks in the top level one, doesn't find it, and raises an error. I tried mysite/mysite/settings.py with the parent mysite's directory in the system path, but that gave the same error.

ImportError: Could not import settings 'mysite.mysite.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named mysite.mysite.settings

To get more info on this error, check How do I stop getting ImportError: Could not import settings 'mofin.settings' when using django with wsgi?

I'm just wondering why would Django make the default structure so difficult and if renaming stuff is the best way out? Seems it could be sloppy and an error-prone way of doing things. Not sure how many places I'd have to change the name if I did start renaming stuff. Seems dangerous.

like image 806
User Avatar asked Jun 27 '14 01:06

User


People also ask

Where to store files in Django?

By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you're using these defaults. However, Django provides ways to write custom file storage systems that allow you to completely customize where and how Django stores files.

How do I access Django media files?

Unfortunately, the Django development server doesn't serve media files by default. Fortunately, there's a very simple workaround: You can add the media root as a static path to the ROOT_URLCONF in your project-level URLs.

How do I import files into Django?

Django provides built-in library and methods that help to upload a file to the server. The forms. FileField() method is used to create a file input and submit the file to the server. While working with files, make sure the HTML form tag contains enctype="multipart/form-data" property.


1 Answers

The key point to understand is that the top-level mysite is not a Python package (note that there's no __init__.py). It's just a regular folder that's used for organizational purposes. (That's why mysite.mysite.settings would never work, regardless of the PYTHONPATH.)

All you need to do is make sure that the top-level mysite directory is the only thing from this project on your PYTHONPATH. Once you do that, everything should work the way you expect it to. (If you find the double-naming confusing, though, you certainly can, with some care, rename things.)

like image 185
Kevin Christopher Henry Avatar answered Oct 20 '22 08:10

Kevin Christopher Henry