Django + Angular 6 example | Django Rest Framework + MySQL CRUD example – Part 3: Angular Client

django-angular-6-django-rest-api-mysql-angular-django-project-structure-feature-image

This tutorial is part 3 of Django-Angular-MySQL series. Today, we will create Angular Client to make HTTP request & receive response from Django Server.

>> Part 1: Overview
>> Part 2: Django Server

Video

Angular Client Overview

Goal

The image below shows overview about Angular Components that we will create:

django-angular-6-django-rest-api-mysql-angular-client-architecture
Project Structure
django-angular-6-django-rest-api-mysql-angular-project-structure

We have:
– 4 components: customers-list, customer-details, create-customer, search-customer.
– 3 modules: FormsModule, HttpClientModule, AppRoutingModule.
customer.ts: class Customer (id, firstName, lastName)
customer.service.ts: Service for Http Client methods
app-routing.module.ts: Routing configuration

Setup Angular Project

Create Angular Project

Run command: ng new AngularDjango.

Create Service & Components

On Project folder, run commands below:
ng g s customer
ng g c create-customer
ng g c customer-details
ng g c customers-list
ng g c search-customers
On each Component selector, delete app- prefix, then change tslint.json rules"component-selector" to false.

Implement Angular Client App

Data Model

Create new file named customer.ts:

Data Service

customer.service.ts

Components
Customer Details

customer-details/customer-details.component.ts

customer-details/customer-details.component.html

List of Customers

customers-list/customers-list.component.ts

customers-list/customers-list.component.html

Create Customer

create-customer/create-customer.component.ts

create-customer/create-customer.component.html

Search Customers

search-customers/search-customers.component.ts

search-customers/search-customers.component.html

Add Router

app-routing.module.ts

And AppComponent HTML for routing:app.component.html

App Module

app.module.ts

Run & Check Results

– Run Django server with command: python manage.py runserver.
– Run Angular App with command: ng serve.

– Open browser for url http://localhost:4200/:

Add Customer:

django-angular-6-example-django-rest-api-mysql-angular-add-customer

Show Customers:

django-angular-6-django-rest-api-mysql-angular-show-customers

Click on Active button to update Customer status:

django-angular-6-example-django-rest-api-mysql-angular-update-customers

Search Customers by Age:

django-angular-6-example-django-rest-api-mysql-angular-search-customers

Delete a Customer:

django-angular-6-example-django-rest-api-mysql-angular-delete-customer

Delete All Customers:

django-angular-6-example-django-rest-api-mysql-angular-delete-all-customers

Source Code

Angular-Django-MySQL-example-Angular-Rest-Client

By grokonez | February 23, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*