Build and Use On Django:

  • in the terminal, use npm run build

    1. [root@localhost twittme-web]# npm run build

    it will save everything in /twittme-web/build directory.

  • delete all files in /static/js

  • and cut all files in /twittme-web/build/static/js to replace

  • delete all files in /static/css

  • and cut all files in /twittme-web/build/static/css to replace

but because file names are not perfectly match:
image.png
we need to do some modifcations.

  • find last 2 script pairs in /twittme-web/build/index.html
  • copy and paste them in Python/reactjs/Twittme/templates/react/js.html to replace 2 scripts before ```bash

  1. - run python collectstatic to all static files to /static-root directory
  2. ```bash
  3. [root@localhost Twittme]# python3 ./manage.py collectstatic
  4. You have requested to collect static files at the destination
  5. location as specified in your settings:
  6. /root/SourceCode/Python/reactjs/Twittme/static-root
  7. This will overwrite existing files!
  8. Are you sure you want to do this?
  9. Type 'yes' to continue, or 'no' to cancel: yes
  10. 8 static files copied to '/root/SourceCode/Python/reactjs/Twittme/static-root', 164 unmodified.
  • save functions as templates in /templates/tweets directory
    • create /templates/tweets/detail.html
    • create /templates/tweets/list.html
    • create /templates/tweets/profile.html
  • copy content in var tweetEl in /twittme-web/public/index.html
  • paste into /templates/tweets/detail.html and modify it as the content of that file ```html {% extends “base.html” %}

{% block content %}

{% endblock content %}

  1. - copy content in /templates/react_via_dj.html and paste into /templates/tweets/list.html
  2. ```html
  3. {% extends 'base.html' %}
  4. {% block content %}
  5. <div id='tweetme-2'></div>
  6. {% endblock content %}
  • copy profile info /twittme-web/public/index.html
  • paste into /templates/tweets/profile.html and modify ```html {% extends ‘base.html’ %}

{% block content %}

{% endblock content %}

  1. - add 3 corresponding views in /tweets/views.py
  2. ```python
  3. def local_tweets_list_view(request, *args, **kwargs):
  4. return render(request, "tweets/list.html")
  5. def local_tweets_detail_view(request, tweet_id, *args, **kwargs): # pass tweet_id
  6. return render(request, "tweets/detail.html", context={"tweet_id": tweet_id})
  7. def local_tweets_profile_view(request, username, *args, **kwargs): # pass username
  8. return render(request, "tweets/profile.html", context={"profile_username": username})
  • import and apply those views in /Twittme/urls.py ```python from django.contrib import admin from django.urls import path, re_path, include from django.conf import settings from django.conf.urls.static import static from django.views.generic import TemplateView

from tweets.views import (

  1. # home_view,
  2. # tweet_detail_view,
  3. # tweet_list_view,
  4. # tweet_create_view,
  5. # tweet_delete_view,
  6. # tweet_action_view,
  7. # new
  8. local_tweets_list_view,
  9. local_tweets_detail_view,
  10. local_tweets_profile_view,

)

urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘api/tweets/‘, include(‘tweets.urls’)),

  1. # new
  2. path('', local_tweets_list_view),
  3. path('<int:tweet_id>', local_tweets_detail_view),
  4. path('profile/<str:username>', local_tweets_profile_view),
  5. # path('', home_view),
  6. # path('react/', TemplateView.as_view(template_name='react_via_dj.html')),
  7. # path('tweets', tweet_list_view),
  8. # path('create-tweet', tweet_create_view),
  9. # path('tweets/<int:tweet_id>', tweet_detail_view),

]

if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

  1. - disable DEFAULT_AUTHENTICATION_CLASSES in /Twittme/settings.py
  2. ```python
  3. if DEBUG:
  4. DEFAULT_RENDERER_CLASSES += [
  5. 'rest_framework.renderers.BrowsableAPIRenderer',
  6. ]
  7. # DEFAULT_AUTHENTICATION_CLASSES += [
  8. # 'Twittme.rest_api.dev.DevAuthentication'
  9. # ]

to test, runserver and access localhost:80 (port 8000 in virtual machine):
image.png
image.png
image.png
all functions in reactjs are fully functional in django page here.