This is a blog project with react.js/next.js frontend, django backend, powered by linux, nginx, gunicorn, redis in docker containers.
Frontend: Next.js (React.js) - server Node.js
Backend: Django (python framework)
Cache system: Redis
Web server: Nginx
WSGI (Web Server Getaway Interface): Gunicorn
Database: postgresql
docker-compose up
docker-compose up -d
(runs in background)
access to localhost:5555
access to /api/ or /admin/ => django application
access to everything else => Next.js application
/static/ => specified static folder
/media/ => specified media folder (user uploaded media)
frontend: create a .env.dev file in frontend folder according to .env.sample.
backend: create a .env.dev file in backend folder according to .env.sample.
- add
DJANGO_SETTINGS_MODULE=blog.settings
in your .env file. - Do NOT add "init.py" file under test folder. Otherwise, pytest can't find modules for some reason.
- start pytest
docker container exec -it backend sh -c "pytest"
- if "backend/countries/fixtures/countries_fixtures.json" doesn't exist, do the following:
docker container exec -it backend sh -c "python countries_data_convert.py"
this will create the countries_fixtures.json file. - load the data from the json file
docker container exec -it backend sh -c "python manage.py loaddata countries/fixtures/countries_fixtures.json"
** load all the initial data at once**docker container exec -it backend sh -c "python3 manage.py loaddata /fixtures/.json"
I created a script to create a super user in one command.
Edit email and password in backend/scripts/add_superuser.py
docker container exec -it backend sh -c "python manage.py runscript add_superuser"
I created a script reset migrations in dev environment.
- drop the database.
- remove all the migration files.
- make new migration files and migrate again.
docker container exec -it backend sh -c "python manage.py runscript reset_migrate"
or
docker container exec -it backend sh -c "python cmanage.py resetmigrate"
docker container exec -it backend sh -c "python cmanage.py startapp app_name singular_app_name"
*singular_app_name is optional
This command creates a folder and scripts including default models ...etc like always but also..
basic serializers.py, test file in tests folder, urls.py, add basic model views script in views.py
It also adds the app in the INSTALLED_APPS in the settings.py!!
docker container exec -it backend sh -c "python manage.py runscript add_api --script-args app_name singular_app_name"
*singular_app_name is optional. If it is empty, singular will be app_name without last plural "s" if any.
You should consider caching the result of a request when the following cases are true:
- rendering the page involves a lot of database queries and/or business logic,
- the page is visited frequently by your users,
- the data is the same for every user,
- and the data does not change often.
CACHE_TTL = getattr(settings, 'CACHE_TTL', DEFAULT_TIMEOUT)
@cache_page(CACHE_TTL)
def get_posts():