Login / Autos CRUD
This assignment is to build a fully working CRUD (Create, Read, Update, and Delete) application to manage automobiles and their makes (i.e. Ford, Hyundai, Toyota, Tata, Audi, etc.).
This application will be based on this folder in the samples repo:
https://github.com/csev/dj4e-samples/tree/main/autos
Do not clone this repository for this assignment. You will make a new
project and application in your django_projects folder and use this application
as sample code.
This application will be similar to:
The login information is as follows:
Account: dj4e-crud
Password: dj4e_nn_!
The 'nn' is a 2-digit number that by now, you should be able to easily guess.
Making a New Application
Make sure you are in your virtual environment and go into your django_projects folder
and start a new application in your mysite project (this project already should have the 'hello'
application from a
previous assignment).
cd ~/django_projects/mysite
python manage.py startapp autos
Extending the home (i.e. main) page
Since we will build a number of applications in this project, we will use the home
application to provide convienent urls to switch between applications.
And you should have a file mysite/home/templates/home/main.html that has the text for the top-level page.
You can keep the "Hello World" text in the page somewhere.
Add a link to the "/autos" url in mysite/home/templates/home/main.html and anything else the autograder needs:
<li><a href="/autos">Autos CRUD</a></li>
It is a list because we will be adding more applications in future assignments. :)
Building the Autos Application
The essense of this task is to adapt the code from:
https://github.com/csev/dj4e-samples/tree/main/autos
and make it work in your autos project. As always there is a lot of code in dj4e-samples - be careful
copying - and only copy code when you know why you are copying it. Go slowly.
Here are some tasks:
-
Go into your
dj4e-samplesfolder and do agit pullto get the latest version of the samples code. -
Create
mysite/home/templates/registrationfolders and copy the (<a href="https://github.com/csev/dj4e-samples/blob/main/home/templates/registration/login.html" target="_blank">login.html) template fromdj4e-samplesintomysite/home/templates/registration/login.html. -
Copy the file from
dj4e-samples/home/templates/base_bootstrap.htmlinto yourmysite/home/templates/base_bootstrap.html- this will be used in yourautos/templatesand make our HTML look better by applying the Bootstrap and other styling libraries. -
Edit
mysite/mysite/settings.pyadd the autos application to the list ofINSTALLED_APPS. You can follow the pattern of theHomeConfigline in that file. -
Edit
mysite/mysite/urls.pyand add theaccounts/path so you can use the Django built in login features. (Authentication Views). Also editmysite/mysite/urls.pyto routeautos/urls toautos/urls.pyfile.path('accounts/', include('django.contrib.auth.urls')), # Add path('autos/', include('autos.urls')), # Add -
Edit the
autos/views.pyfile to add views for the list, create, update, and delete views for both autos and makes based on the sample code. -
Create/Edit the
autos/urls.pyfile to add routes for views for both autos and makes based on the sample code. -
Create/Edit the
autos/forms.pyfile to add the form for MakeForm -
In your
views.pyfile, you can start out using theMakeviews from the sample code, but once you have the application working, you should come back and rewrite theMakeviews using the same patterns as theAutoviews. If you switch to the pattern in theAutosviews and use the generic edit views on yourMakeviews you no longer need to have aMakeFormorforms.py. You can either write the long version of the views or the short version of the views - the short version is easier to code but more challenging to understand because it relies so heavily on a complex parent object and inheritance. - Edit the
autos/models.pyfile to add Auto and Makes models with a foreign key from Autos to Makes.
-
Run the
python manage.py checkuntil you see no errors -
Run the
python manage.py makemigrationsuntil it has no errors. Sometimes when you make changes tomodels.py, the makemigrations will pick up on the changes and ask you for example if you want to rename a field. Sometimes you make a change to yourmodels.pyandmakemigrationsgets stuck or lost. If migrations gets stuck, you might need to start with a fresh database. -
Run the
python manage.py migrateto create the database. -
Edit
autos/admin.pyto add the Auto and Make models to the Django administration interface. -
Run the
python manage.py checkuntil you see no errors -
Create a superuser so you can test the admin interface and log in to the application.
-
Create the necessary template files in
autos/templates/autosto support your views. Note that the second sub folder undertemplatesis there to make sure that templates are not inadvertently shared across multiple applications within a Django project. -
Find the line in your
base_bootstrap.htmlthat looks like this:<meta name="dj4e-code" content="99999999">and change the
9999999to be "missing"
Make sure to check the autograder for additional markup requirements.
Things that can go wrong
If you ever get a 405 error on a Django page it probably means that you
have defined a view class that does not have a get() method.
For example if you meant to say this:
class AutoUpdate(LoginRequiredMixin, UpdateView):
model = Auto
fields = '__all__'
success_url = reverse_lazy('autos:all')
But instead you did:
class AutoUpdate(LoginRequiredMixin, View):
model = Auto
fields = '__all__'
success_url = reverse_lazy('autos:all')
(i.e. you extended View instead of UpdateView) - the result is that there
is no def get(self, request): in your view.
So you get the
405 HTTP status code (invalid method)
when you navigate to the URL that forwards to the view.
Manual Testing
It is always a good idea to manually test your application before submitting it for grading. Here is a rough outline of the steps that the autograder will take to grade your application. You should run them by hand before running the autograder and make sure they work without error.
- Log in
- Add a make
- Add an auto selecting the make
- View the makes
- Update the make and press 'Cancel' - Should go to the auto list list
- Update the make, make a change and save it - Should go to the auto list
- The make for the auto in the list should be automatically updated
- Update the auto, change its nickname and save it - it should be updated in the auto list
- Add a second make
- Add a second auto using the second make
- View the autos - make sure the second car and make are present
- Delete the second make - the auto corresponding to the make should go away automaticallr (Cascade in action)
- Delete the first auto