Problems with Windows 10/11 and Mosh’s Django Course 1 (Fixes Included)
This is for all the students like me who struggled at various points with some of these issues on windows and how I fixed them.
Setting Intepreter Path:
To all those coming in and using vscode on windows, when setting your interpreter path, its in C:\Users\User.virtualenvs\storefront-XXX\scripts\python , not \bin\python. Obviously substitute “User” with your username, and “XXX” with your env.
This also doubles up for those looking for the “bin” directory when hes showing where its at in file explorer, its in “scripts”.
The Debug Tool Bar:
This one was super annoying, I could not for the life of me get this dang toolbar to work, so, some tips, and don’t be like me and say screw it and skip this step in the lessons only to find yourself needing it to follow along in later lessons
- Understand that in the settings.py file in the main folder titled “storefront” if you are following along 1:1, that there is a setting called DEBUG, it is set to TRUE, for future reasons know that if you set that to false, you will need to whitelist your IP in the ALLOWED_HOSTS = [] area.
Thats just a note for when you get further.
But for now, why is not showing? Where there is a bunch of reasons one, the mimetype isn’t allowed in modern browsers by default, we need to add right underneath “DEBUG = TRUE” the following (This is one of the main things that breaks it):
if DEBUG:
import mimetypes
mimetypes.add_type(“application/javascript”, “.js”, True)
Next, ensure right underneath ALLOWED_HOSTS=[] we paste our internal IP’s like in the documentation:
INTERNAL_IPS = [‘127.0.0.1’ ]
Next ensure you have added it to our INSTALLED_APPS list, I put mine at the top so my list looks like the following:
INSTALLED_APPS = [
‘debug_toolbar’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘store’,
‘tags’,
‘likes’,
‘playground’
]
Next in the middleware section, this isn’t mentioned at first and I had to seek this out on stack overflow but this list has a load order, and if you place something in the wrong order, your middleware can actually be intercepted by something else, the common advised area is to place it right underneath ‘django.middleware.common.CommonMiddleware’, so my entries look like this:
MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘debug_toolbar.middleware.DebugToolbarMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’
]
Next up, and another likely main culprit, we have to change a regkey. Now this one is a bit complicated for people that may not be familiar with the windows OS and it’s regkeys, and you can def do damage to your OS playing around in here, so make sure you follow this bit of instructions EXACT
hit your windows key and type ‘regedit’ no quotes, then hit enter, this will open up what is known as your ‘hive’ of regkeys, you will note that this is very much structured like a folder system.
At the top you should see: HKEY_CLASSES_ROOT, this is the ‘folder’ we will be expanding, and we are looking for another folder called ‘.js’ no quotes. Click on this folder and to your right you will see Content Type and the data next to it will say “text/plain” no quotes, double ckick on Content Type, and change this value to “text/javascript”, no quotes.
Finally lets double check your urls.py in the storefront directory, it should look like so:
import debug_toolbar
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘debug/’, include(debug_toolbar.urls)),
path(‘playground/’, include(‘playground.urls’))
]
Make sure you save the files you edited in vscode, close the regeditor, restart your server, and if you are still not seeing it, its probably your cache, navigate to the page again but do it in incognito mode or your equivalent for your browser and you should see it.
You welcome.
mysqlclient broke af
This one was super annoying because it causes you to do something thats going to make you quite uncomfortable.
If you are like me, you likely installed the latest version of python (at the time of this writting 3.10), and at the time of this writting, the connector for mysql is only compatible with 3.9 and under. So yea, your virtual env you have been using, is a no go for the rest of this project. So you may ask, how do I recover all my crap is on 3.10! Well don’t worry I will show you a quick work around.
First, go and install 3.9.7 (At the time of this writing, this was the most current release for 3.9). Once you have it installed and added to your path like last time we are ready to move to the next step.
First lets make a backup of our requirements for our project, we can do this by issuing the following command within our terminal thats in our virtual env: ‘pip freeze > requirements.txt’ do that command without the quotes, this will save a copy of all the packages we have installed, good now on to the next step
Exit your current venv by issueing the following command on the terminal ‘exit’ no quotes.
This will exit your virtual env.
Next up we need to make ourselves a new virtual environment using the 3.9.7 interpreter, we can do that by issuing the following command in our terminal: ‘pipenv --python 3.9.7’ no quotes.
This will rebuild a brand new virtual environment. Scary… but don’t worry all good.
Now lets connect to said virtual environment by issueing ‘pipenv shell’, this is our new virtual env using the correct interpreter thats compatible with the mysqlclient.
Next we need our modules we had previously installed back, we can do that by issuing the following command ‘pip install -r requirements.txt’
yay, we have them all back now.
Now finally, we can issue ‘pip install mysqlclient’ and it will install fine. Now you can migrate!