Misc Django 4 2 Upgrade 2023 08 en
Misc Django 4 2 Upgrade 2023 08 en
Welcome to
my summer 2023 update for Django for Everybody.
The primary thing we're going to do
is to move from version
4.0 to 4.2 of Django.
I pretty much every summer had been updating this course.
In 2021, we went from Django 2.1 to 3.0,
2022, we went from 3.0 to 4.0.
Now this summer I'm going to Django 4.2.
The nice thing about it is the.2 releases of Django,
they tend to support them longer.
I should be able to have this Django
4.2 working until 2026.
Then maybe in 2026,
I will be able to go to Django 5.2,
which is the next long term support version of Django.
It's nice to keep you up to date,
keep using later versions of Django.
Nothing I did this summer as I moved from
Bootstrap 3 to Bootstrap 5.
What happens when we put in a new Django release?
The nice thing is that
going from Version 2 to Version 4,
over the last couple of years,
there have not been to
many breaking changes of our Django code itself.
They have to do things like keep up
with new versions of Python because
Python has been advancing pretty
rapidly over that same time period.
If they use Python 3.4,
and a couple of years they can't use Python 3.4.
The other thing that they've been doing is in a way
to compete with other web frameworks.
They're adding asynchronous support
to improve performance.
Way Python applications talk
to Port 80 basically was through this WSGI,
Web Server Gateway Interface.
This was a performance limitation
in that there was just one thread.
Now, there were ways that you could have
multiple instances of your Django application
talking to the database.
You can have multiple threads.
You have to have multiple WSGI.
If you've ever done Python anywhere and
you've seen how many threads do you want?
What they're doing is making multiple copies and then
distributing the requests through those copies.
But if you only have one thread and
your server side code takes
awhile then it actually is locked up.
If you had things that start
getting slowing when your system gets heavily loaded,
popular, it's going to slow down.
There's ways to make
this WSGI-based connection to the outside world work.
But Python 3.5 had this thing called async await.
It allowed for a thread
that was going to do something a long time to say,
let's start this thing and I'm going to
do something else and then later I'm going
to wait for this other thing to finish.
They created this new way of talking to, in a sense,
the operating system and things
like Engine X or Apache called the AGSI,
the Asynchronous Gateway Interface.
A lot of the changes from 2, 3,
and 4 have been to improve
the ad and then improve over time the AGSI code.
Luckily we have been using WGSI.
Python anywhere only supports WGSI.
We've been insulated from most of those improvements.
In future versions,
we might see more significant changes to
our code as the Django releases change.
But so far for the past couple of years,
you'll see at times I have recorded lectures that are
Django 2. They're not that different.
I mean, they're really almost not different at all,
but we'll see what's going forward.
But I should be able to use
this Django 4.2 for a couple of years right now.
Let's talk about the impacts in the courses.
The first impact is in Course 1.
First assignment of Course 1.
You probably already done this.
That assignment make inside a Python anywhere it makes
a virtual environment which chooses a Python version,
chooses a Django version,
and then with DJ4E samples that assault stalls
a set of prerequisites requirements for.txt.
That's a bunch of library software,
also known as dependencies that are
needed for our applications.
Now, you don't know this is happening.
I'm just having you type commands and they work.
What you've done is you've configured
your virtual environment with unnecessary things.
Literally, you're going to
use throughout the rest of the course.
That's why I do that on the very first assignment.
I've got all up.
We get the virtual environment made,
we get all the requirements properly with PIP.
PIP is the Python Installed Program
that goes and finds
these dependencies and sticks them in there.
At the end of the first course,
you are pretty much set with your environment.
Then we have a Django project
and we put different applications in,
and you don't change your Django version.
You could literally go for two or three years with
an old version of Django and the auto-graders won't care.
You've pretty much, the moment that you,
and you can look,
well, there's a thing,
Python minus M Django minus version,
which I'll tell you what version that you're running.
Make sure you activate your
virtual environment, of course,
so you are choosing or my documentation caused you
to choose a Django version
in the first assignment of Course 1.
Now, what happens at the end of Course
3 is you do this adds as one assignments a departure,
from all the other ones.
You start with the Courses 1 and 2 and most of Course 3,
and you just throw in a little bit
more into your Django project
in autos and cats
and session and hello and all that stuff.
You weren't changing Django versions
at all. It didn't matter.
You're just throwing more code into
your Django project and
it was using all the requirements in
the virtual environment, all that stuff.
What happens is, is that an ads one I have is a bunch of
instructions that have you pull data from
a poll code from my DJ free samples repository.
This is where, but I have to give you
two versions of the my dj4e sample code repository.
First half of class,
if you look at my sample code,
it doesn't matter between Django
4.2 and Django 4.0 for most
of the changes are in settings.py and urls.py.
If you're using Django 4.0 you just
have to use the Django 4.0 of DJ free samples,
and if you're using Django 4.2 you've got to use
the Django 4.2 of DJ free samples.
I don't recommend that you change your Django version.
Just if you started on 4.0 finish on 4.0.
If you started on 4.2 finish on 4.2.
At the moment, you're setting up the ads assignment.
You have to figure out what version you're using
and then switch to
the right version of the DJ free samples.
This is all documented in the assignment description.
Let me show you that assignment description.
Here's the instructions for
the ads one assignment and again,
this is an assignment that's in Course 3 towards
the end. I've had to change it.
It didn't before and there was
just Django 4.0 Version of this code.
Let me make this a little bit bigger so you can see it.
Building classified ad website.
Eventually, you're going to the rest of the course,
you're going to be working on this one application.
But the key thing is you're copying a bunch of code,
autumn Mine sample application.
During the fall of 2023,
I'm switching a sample code to 4.2.
You may have to switch the sample code
back to 4.0 before you start copying it.
Again, I don't recommend that you try to upgrade to 4.2.
I just think, I just want you to grab the right sample.
Now, the key thing is,
is you've got to figure out
which version of Django that you're running.
You go into your virtual environment and you just say,
Python, what version of Django am I running?
It says 4.0 or it says 4.2.
Then what you need to do is you need to
get the right version of
the sample code that corresponds to your Django version.
Now you probably did this in Assignment
1 where you clone the DJ free samples.
You probably don't have to do these two things.
What you do here is you go into your samples.
If the version that you looked at is 4.2,
you want the main branch.
Then if the version is 4.0,
you want the Django for our branch and this is get,
it gets amazing and I'm able to
completely keep to complete versions of this.
I can even fix the Django for
old version if I find a bug in it.
While also then separately fixing the 4.2.
This Git checkout main DJ
free samples or git checkout deep Django 4.0,
this is the one or two things that you choose.
Check your Django version and then make sure
you start before you start ads one,
that you're looking at the sample code that
correspond to
the Django version that you're working with.
You can see why I'm trying to tell you,
don't upgrade your Django.
Just keep on cruising,
just get the sample code right.
Then once you've done one of these two things,
you do git pull to make sure
you've got the latest version,
and then you install the requirements.
This will take all of
the little libraries that are necessary for
everything to work and install the right requirements.
If you're in a 4.2, it's going to
install the 4.2 version of the prerequisites,
and if it's in a 4.0,
it is going to install the 4.0.
You have switched, that file is different.
You can actually, if you really get crazy,
you can switch between main and Django 4.0.
You can look at the file, you can see how it's different.
But don't get too crazy about this.
Just get it working.
Then you want to run Python managed.py
check over until it doesn't complain.
Until you don't get a traceback.
You don't get missing library Python extensions.
Students make a mistake.
As soon as this doesn't work, they go boom,
and then they just start installing a different version,
and then we have to undo a whole bunch of stuff.
Just relax here, get it right.
If you run a Django 4.2,
and you run these things,
pip install, and you get
the right version of the sample code.
Python managed check, make sure that
your virtual environment configuration,
which is your Python version,
Django version and requirements versions,
pip versions, and
the code in DJ free samples are matching.
If you were to carefully look at the difference between
the Django 4.0 and the Django 4.2 code,
settings.py is where most of the changes are.
Even those changes are pretty tiny,
the crispy forms is
the only thing I had to make a new lecture for.
That's because crispy forms decided to change.
It was a combination of Django
4.2 and my move to Bootstrap 5.
The settings.py is subtly different.
You don't have to understand those differences.
You can go click in
my Git repo if you really want to look at them.
But if you're in
Django 4.0 and get the sample codes Django 4.0,
you'll copy settings.py,
and I'll do all your stuff, and you get the right thing.
If you're using Django 4.2,
and you get the sample code
4.2, you'll get the right thing.
But the crispy forms, actually,
it talks through all those settings.
So I had to make a different lecture for Django 4.2.
But really because there were
so few real other changes other than crispy forms,
I didn't have to re-record all my four or even 3.0.
Some of my lectures are just about 2.0.
But most of those just work.
Hasn't just been a lot of changes.
Thankfully, Django has kept all of
their APIs pretty stable.
In conclusion, we have to update this course.
We can't just leave it alone.
We can't just sit on Django 2, I mean,
for me it would have been a lot
easier to just keep Django 2 forever.
But it just means every summer I got to spend
a bunch of time updating all this stuff.
So far, it hasn't been too bad.
I mean, there's little things and we
help you through them, and we get through them.
Auto-graders in the course don't care
what Django you're using.
Don't get all hurried to go to Django 4.2.
The trickiest part that I've had to do is get
you both the Django 4.0 and 4.2 version of
the DJ free samples at the same time and then give you
the instructions to figure out which one you're
supposed to use and then how to use the one.
I hope that you found this little video useful.
I think this is going to go really smoothly.
We'll be watching the forums and
trying to make sure you get the help you need,
if you get stuck and stuff.
If you get stuck, it's not that hard to fix it.
You just have to slow down
and not just go and start deleting stuff.
Who knows what you're going to do?
Changing your file.
It's not that hard to fix it if you get things and stuff.
I've been going in my Python anywhere
between 4.0 and 4.2 and back-and-forth.
I've been doing a lot of testing at teaching systems,
and we're all testing for.
Thanks for listening and I hope to
see you online. Cheers.