Confused about where to create my parent directory reusable app in Advanced tutorial: How to write reusable apps

Hi,

I am a bit confused about where to create my parent directory reusable app in Advanced tutorial: How to write reusable apps. My django-polls (I didn’t use the name djangotutorial) directory structure looks like the following:

- django-polls/ # the top level directory root of our Git repository which contains our django_polls project and polls app
    - .git/
    - .vscode/
    - django_polls/
        - __pycache__/
        - __init__.py
        - asgi.py
        - settings.py
        - urls.py
        - wsgi.py
    - polls/
        - __pycache__/
        - migrations/
            - __pycache__/
            - __init__.py
            - 0001_initial.py
        - static/
            - polls/
                - css/
                    - style.css
                - images/
                    - david-clode-J_5xvghAvmc-unsplash.jpg
        - templates/
             - polls/
                - detail.html
                - index.html
                - results.html
        - tests/
            - __pycache__/
            - __init__.py
            - test_question_model_tests.py
        - __init__.py # which is what makes Django recognize the polls direcotry as an app
        - admin.py
        - apps.py
        - models.py
        - urls.py
        - views.py
    - templates/
        - admin/
            - base_site.html
            - index.html
    - venv
    - .env
    - .gitignore
    - db.sqlite3
    - manage.py
    - requirements.txt

Am I to understand that I should create this parent directory for my polls app called django-polls outside of my top-level Django-polls directory? I also noticed that there are a lot of packages containing either just django-polls (already one with that name that exists), so I would have to use some other variant of django-polls anyway. Or would that be a problem? But going back to the original question, am I to create that parent directory for the reusable polls app outside of my django-polls directory? Thanks!

I found my answer. it is to be outside of my django-polls directory. Which means that I will have to change the name of my current top level django-polls directory. Hopefully this will help someone else who might be confused by the same thing.

No, you don’t need to do this.

You can create another directory - perhaps named something like packages, and then create your django-polls directory within that directory.

So then the packages directory could be in the same directory as my django-polls, and inside packages I would have django-polls. Is that correct? Thanks @KenWhitesell! That part of the tutorial is a bit confusing!

Yes. Or the packages directory could be at the same level as the parent directory, or anywhere else you want to put it.

It actual directory doesn’t matter - as long as it’s not in your django-polls project.

Got it. Thanks! But if I wanted to continue to develop this project and app in the project, could I keep a copy of the app in the original project? I should be able to, right? And then do the same process in the next release, right? Thanks again!

You could, but you don’t necessarily want to.

The idea of the “reusable app” is that you “pip install” it in your virtual environment, then add it to your INSTALLED_APPS setting - you don’t need to keep the code in your project. (This is covered in the Using your own package section.)

Now, wanting to continue development on the package does create some additional work.

I’m aware of at least three different ways of handling this situation.

  • Yes, keep a copy in your project, this changes your development process:

    • Copy it to the external directory when you make changes,
    • create the package,
    • and then test it in a separate copy of your project
  • Use a symlink to link the polls directory in your project. This saves the need to copy the source to the external directory.

  • Do your development in the external copy, then copy it to your development environment for non-package testing, then create the package and test again.

  • Do your development in the external copy, then only test by creating the package and testing in your project copy.

None are “trivial”, they all add “friction” to the development cycle - but it’s not too bad.

Thanks so much @KenWhitesell. I am totally new to this.But packages are constantly being updated and maintained. So of course it is more work, but it does demonstrate thet it is being maintained. Right? But of course the best thing would be to do this after developing the app to the point where one thinks it is ready for release. I was just simply following the tutorial, and wanted to learn how to create a reusable app using this polls app as a great way to start. But then I realized I wanted to test some other things out, taking it a step(s) further. But re-releasing could be a good learning experience too using this app as a test case. Thanks again for all your help!

I see why the Django Polls tutorial set up the project and app the way it did. Not having an index page at the project level was a way of showing the decoupling of polls. Am I right or not? Thanks!

1 Like

just to let you know @KenWhitesell I went back and did further work on the polls app and am now satisfied with it. Won’t want to change it. Just as is. Added styling and a couple of features based on what I learned in the tutorial. So glad I did it! AND, because of the lack of a top level home page, I created another reusable app to satisfy that need. A Home Page reusable app. They really look great together. Now will try and package them both (separately). Thanks again!

1 Like