Skip to content

benjaminas2018/react_django

Repository files navigation

pip install django djangorestframework django-rest-knox
  • Create Django project
django-admin startproject leadmanager
  • change the directory to leadmanager/
python3 manage.py startapp leads
  • go to leadmanager/leadmanager/settings.py
- *add new app into INSTALLED_APPS in the settings.py file*
 INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'leads',
    'rest_framework'
]
  • the sqlites is using for the learning, change according to the real database, you can use mysql etc.

  • after APP created, you should define the app models for database:leadmanager/leads/models.py

class Lead(models.Model) : name = models.CharField(max_length = 100)
    # Avoid multiple emails,
    # so the unique set to True
    email = models.EmailField(max_length = 100, unique = True)
    # enable the message could be empty,
    # set the blank = True
    message = models.CharField(max_length = 500, blank = True)
    created_at = models.DateTimeField(auto_now_add = True)
  • after the modes.py changed, you should run the below commands to create database
python3 manage.py makemigrations leads
python3 manage.py migrate leads
  • The output should be like:
Operations to perform:
  Apply all migrations: leads
Running migrations:
  Applying leads.0001_initial... OK
from rest_framework import serializers
from leads.models import Lead

# Lead Serializers
class LeadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lead
        # __all__ stands for all fields we defined in leads/models.py
        fields = '__all__'
  • Create api.py under the app: leads
from leads.models import Lead
from rest_framework import viewsets, permissions
from .serializers import LeadSerializer

# Lead Viewset
class LeadViewSet(viewsets.ModelViewSet):
    queryset = Lead.objects.all()
    permission_classes = [
        permissions.AllowAny #could be changed according real requirements
    ]
    serializer_class = LeadSerializer
  • go to the leadmanager folder , change urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('leads.urls')),
]
  • create urls.py under the app: leads
from  rest_framework import routers
from .api import LeadViewSet

router = routers.DefaultRouter()
router.register('api/leads', LeadViewSet, "leads")

urlpatterns = router.urls
  • then start the django website
python3 manage.py runserver

Start the React Part

npm init -y npm i webpack webpack-cli --save-dev

    • Now open up package.json and configure the scripts:
"scripts": {
  "dev": "webpack --mode development ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js",
  "build": "webpack --mode production ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js"
npm i @babel/core babel-loader @babel/preset-env @babel/preset-react babel-plugin-transform-class-properties --save-dev
npm i react react-dom prop-types --save-dev
npm i redux react-redux redux-thunk redux-devtools-extension
npm i axios
    • Problem when run npm install
sudo npm cache clean --force
sudo rm rf node_modules
sudo npm install
    • Authentication Steps
npm i react-router-dom
  • create Regester.js and Login.js under components/accounts, the accounts folder is created first

About

Self_learning_for_react_django

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published