-
Clone git repo:
git clone https://github.com/ibm-developer-skills-network/sfvih-Back-end-Development-Capstone.git
-
Get into the directory by
cd sfvih-Back-end-Development-Capstone
-
Get into Django code
cd djangoserver
-
Install requirements
pip install -r requirements.txt
-
Run the server
python manage.py runserver
-
It will tell you that you have unapplied migrations.
Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into. There are several commands which you will use to interact with migrations and Django’s handling of database schema:
- migrate, which is responsible for applying and unapplying migrations.
- makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.
- sqlmigrate, which displays the SQL statements for a migration.
- showmigrations, which lists a project’s migrations and their status.
-
Create the initial migrations and generate the database schema:
python manage.py makemigrations python manage.py migrate
-
Run server successfully this time:
python manage.py runserver
-
Launch Application
-
Click on Songs and Photos
-
Click on Concerts, no existing Concert present
-
Let's create admin user
python manage.py createsuperuser
- Username:
admin
- Email address: leave blank, simply press enter
- Password: Your choice, or simply
qwerty123
- Username:
-
Run the server again
python manage.py runserver
and goto admin:http://localhost:8000/admin/
-
Enter the admin user details you created in previous step.
-
Now you are in the admin section built by Django.
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.
-
Add a Concert:
- Concert name:
Coachella 2023
- Duration:
72
- City:
Indio, California
- Date:
2023-04-14
- Concert name:
-
Click on
View Site
meny at the top -
Now if you visit
Concerts
, you will see Coachella listed. -
Our Django application is now running, but Songs and Photos are hard coded.
-
Open
concert\views.py
-
See
songs
andphotos
definition.- Retrieve
songs
from a REST endpoint by replacing the code with following:
songs = req.get( "https://raw.githubusercontent.com/captainfedoraskillup/private-get-songs/main/backend/data/songs.json").json() return render(request, "songs.html", {"songs": songs})
- Retrieve
photos
from a REST endpoint by replacing the code with following:
photos = req.get( "https://raw.githubusercontent.com/captainfedoraskillup/private-get-pictures/main/backend/data/pictures.json").json() return render(request, "photos.html", {"photos": photos})
- Retrieve
-
Verify Songs and Photos changes. Visit the Songs section, you will see a longer list of songs, clicking on each will show its Lyrics in a modal dialog. While going into Photos, you will see more than two.
-
Now back to Concerts, click on the concert Coachella we created. You will see an RSVP page.
-
RSVP Page shows you details of the Concert along with an option to either: Attend, Not Attend or no Option
-
. -
If you open
concert_detail.html
, you will see an html form:```html <form action="{% url 'concert_attendee' %}" method="POST"> {% csrf_token %} <input name="concert_id" type="number" value="{{concert_details.id}}" hidden="hidden" /> <div class="input-group mb-3"> <label class="input-group-text" for="attendee_choice">RSVP</label> <select class="form-select" name="attendee_choice" required> {% for attending_choice in attending_choices %} <option {% if attending_choice.0 == status %}selected {% endif %} value="{{ attending_choice.0 }}">{{ attending_choice.1 }}</option> {% endfor %} </select> </div> <input type="submit" class="btn btn-primary" /> </form> ```
-
On
Submit
of this form, the details are sent toconcert_attendee
inconcert\views.py
. -
It does two validations:
if request.user.is_authenticated:
means whether the user is authenticated. Because anonymous users are not allowed to RSVP. Thenif request.method == "POST":
to check whether it is anHTTP POST
event. -
From the
body
of thePOST
method, it takesconcert_id
andattendee_choice
. -
Then it checks, whether a selection was made previously, if yes, then update it. Otherwise insert new selection the databaes for this user.
-
Finally, redirect the user.
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
find . -path "*/db.sqlite3" -delete
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Execute:
python manage.py dumpdata > datadump.json
Next, change your settings.py
to the mysql database.
Finally:
python manage.py loaddata datadump.json
-
build a docker image
docker build . -t concert
-
tag docker image with the correct registry information
docker tag concert captainfedora/concert:v1
The above command tags to
captainfedora
repository on dockerhub with the image name ofconcert
and label ofv1
-
Run the docker image to validate everything is working
docker run -p 8000:8000 captainfedora/concert:v1