Django CRUD App Lab Exercise
Build Your Own Full Stack Django Application
This lab is an opportunity to apply what you’ve learned in building the Django CRUD App, Cat_Collector, to a project of your own.
Use the structure and techniques from Cat_Collector as a guide to create a custom application.
1. Setup Your Django Application
Create Your Application
-
Start a New App: Begin by setting up an application within the Django project you created during the setup phase.
python3 manage.py startapp my_app -
Register Your Application: Add your new app to the list of
INSTALLED_APPSinsettings.py. -
Start the Server: Test your new app connection.
python3 manage.py runserver
Configure the Database
-
Create Your Database: Before adjusting the
settings.py, ensure you create your database.createdb mycoolapp -
Database Settings: Edit the
settings.pyfile to include your local database settings.DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mycoolapp', } } -
Migrate Models: After setting up your model classes in
models.py, make and apply migrations.python3 manage.py makemigrations python3 manage.py migrate
Set Up URL Routing
-
Create URL Configuration: Make a new
urls.pyin your app directory to handle app-specific routes. -
Include App URLs in Project: Modify the project’s
urls.pyto include your app’s URL configurations.from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('my_app.urls')), # Mount the app's routes at the root URL ]
2. Implement Full CRUD for a Model
Develop URL patterns, views, and templates to manage a resource fully. Your application should include views for:
- Home: The landing page
- Index: List all instances of the model
- Detail: Show details of a single instance
- Create: Form to create a new instance
- Update: Form to update an existing instance
- Delete: Handle the deletion of an instance
3. Introduce a Secondary Model with Associations
- Model Association: Create a one-to-many relationship with a second model.
- Manage Resources: Implement full CRUD operations for the secondary model, ensuring resources can be created, read, updated, and deleted.
4. Integrate Authentication
- Enable Django Authentication: Incorporate Django’s built-in authentication system to manage user accounts.
- Admin Dashboard: Utilize Django’s admin to manage user activities.
- Protect Resources: Ensure that routes related to user-owned resources require authentication.
Level Up
5. Introduce a Third Model with Associations
- Model Association: Create a many-to-many relationship with a third model.
- Manage Resources: Implement full CRUD operations for the third model, ensuring resources can be created, read, updated, and deleted.