Please forgive me for this very long question, but I believe it is necessary both for you to be able to help me and for me to be able to understand your directions.
I have two versions of a Django project in git branches master and qs2. I am the only contributor. All the commits in master are also in qs2, and qs2 has 3 additional commits. In both repos, the project level views.py have an identical import of statedict. This is a simple dictionary I created which maps complete state names to state two digit postal abbreviations. The two digit code is in the url, and is passed to the view so as to pull the right content from the right state models. Here is an example:
http://127.0.0.1:8090/co/<slug for whatever resource related to Colorado the user wants>
All of this works as expected in master. But in qs2, PyCharm has a red squiggly line under the import and I get ‘no module named statedict’ when I call runserver. If I comment that out, and then call the site, runserver executes but the browser gives me a 404. Django Debug Toolbar says no request was made and no template was served. I have run git diff but been unable to find a difference related to the imports, let alone an actual cause for this issue.
However - and I think this is significant - in the Django console, (I’m using PyCharm) I can import statedict with no issues on both repo branches. The reason this is significant is that whenever I get ‘module not found’ or ‘ImproperlyConfigured’, or ‘apps not ready’ errors, I can almost always fix them by changing PyCharm’s run configuration to use the Django Console instead of the PyCharm run window. Because of that history, I infer that somehow PyCharm’s default run config is outside my virtualenv, but I do not claim to have any depth of understanding about that.
Now, it would be easy enough to just ditch qs2 and start over with a new branch off master, but I would like to know and understand how and why this happened so I can avoid repeating this mistake. It was in the course of this pursuit that I stumbled upon a new problem:
I can go straight to the detail page on any instance of this model in master. However, if I click “view_on_site” in the admin, I get ‘no reverse match’ in both repos. It is clear from the error message that the redirect inside view_on_site is not using my slug based get_absolute_url(), but an algorithm of their own making that essentially treats the instance url as an instance of generic foreign key, trying to match on the primary key. Thus, the failure is not a mystery, but fixing it is.
I have now come to believe that this ‘no reverse match’ / ‘view on site’ issue is separate and apart from the statedict import and url issue, but I didn’t know that at first, so onward I plunged.
This led me to the sites framework, SITE_ID, and django.contrib.sites. The docs and other sources say these things are used in a lot of places, including view on site, and recommend we set it. So I did, and I migrated. And I changed the default name of my site from example.com to localhost. And it didn’t work. My problem remained unsolved.
That finally led me here, specifically to this post: Admin Site's "VIEW ON SITE" URL Incorrect. I had great hope that this seemingly simple over ride solution was the answer I was looking for. I added it to admin.py as instructed … but nothing.
It was at this point I decided to gather up this history and post my own question, which is this very post. In doing so, I went back over a few things, including that failed import of statedict on qs2, and well, guess what? The red squiggly is gone! Somehow, somewhere, all this other stuff I was doing fixed that issue - but which one? I have no clue. And now you see why I put all these seemingly disconnected issues into this one post: they have brought me full circle.
I don’t even know where to go from here, so I have not posted any code. I will let you tell me what you think would be helpful and relevant to see.
As I said at the beginning, I could not only jettison qs2 but also I could just not use view on site. I suspect both my project and I would survive, but I wouldn’t learn anything, either. Thank you for our patience in reading. Please help.