-
Notifications
You must be signed in to change notification settings - Fork 1
Home
aislaapps edited this page Mar 24, 2022
·
9 revisions
Guide to create a Django and Angular Project
This repo's folder structure has a backend and frontend at the same level.
|-project
|-backend
|- Django project
|-frontend
|- Angular project
But this wiki is a guide to create Django project as the main folder, and Angular as a subfolder in a frontend folder.
|- Django project
|- manage.py
|- frontend
|-Angular project
|- api
pyenv
pyenv-virtualenv
python 3.6.8
node.js
angular
pyenv virtualenv 3.6.8 <project name>
this will create your env as (ie: project name=djangular_invoice_messages):
3.6.8/envs/djangular_invoice_messages
3.6.8/envs/djangular_invoice_messages
- close your terminal, and open a new one
- cd to where your .python-version was saved
pyenv activate <project name>
same as:
source /Users/yourhome/.pyenv/versions/3.6.8/envs/<project name>/bin/activate
pyenv local
(this will create a .python-version with your virtualenv version/name)
python -m django -V
(if result is: no command found install django)
pip install Django
django-admin startproject <project name>
(this will create your <project name> folders and your files in it)
In the django project:
cd <project name>
vi .python-version
type 3.6.8/envs/djangular_invoice_messages and save
cd <project name>
python manage.py runserver
http://127.0.0.1:8000/
cd <project name>
python manage.py startapp <app name>
Update overflow.views:
from django.http import HttpResponse
def overflow(request):
return HttpResponse("Overflow views.")
Update/Create overflow.urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.overflow, name='overflow'),
Add your app in the INSTALLED_APPS in settings.py
....
'overflow',
Test run
http://127.0.0.1:8000/overflow/
manage.py migrate
manage.py createsuperuser
http://127.0.0.1:8000/admin
You should be inside <project name>, at the same level of manage.py
if not:
cd <project name>
mkdir frontend
cd frontend
ng new <project name>
Answer Yes to Angular Routing, and choose scss
Create a proxy.conf.json file in the main folder water-overflow
{
"/messages":{
"target" : "http://localhost:8000",
"secure" : false
},
}
cd water-overflow
ng serve
http://localhost:4200/
pip install djangorestframework
pip install django-cors-headers
pip install django-webpack-loader
pip install pytest
pip freeze > requirements.txt
Add the following in INSTALLED_APPS
'rest_framework',
'corsheaders',
'webpack_loader',
Add corsheaders in MIDDLEWARE
'corsheaders.middleware.CorsMiddleware',
Set CORS Settings Just above the STATIC_URL
# CORS Settings
CORS_ORIGIN_ALLOW_ALL = True
Set Angular Path above STATIC_URL
ANGULAR_APP_DIR = os.path.join(BASE_DIR, 'frontend/water-overflow/dist/water-overflow')
Set Static Files Directory below STATIC_URL
STATICFILES_DIRS = [
os.path.join(ANGULAR_APP_DIR),
]
Import serve, and RedirectView
from django.contrib.staticfiles.views import serve
from django.views.generic import RedirectView
Add Urls to Serve your index.html, and staticfiles
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^messages/', include('messages.urls')),
url(r'^$', serve, kwargs={'path':'index.html'}),
url(r'^(?!/?static/)(?!/?media/)(?P<path>.*\..*)$',
RedirectView.as_view(url='/static/%(path)s', permanent=False)),
]
Build your Angular Project
cd frontend/invoice_messages
ng build
Create a blank Repo in github:
aislaapps
To commit all codes from django to frontend folders, we need to remove .git folder in frontend/invoice_messages
Angular has auto git init the folders on creation.
cd frontend/<project name>
rm -rf .git
Git initialise the main folder, and commit:
cd <project name>
Update .gitignore
git init
git add .
git commit -m "Initial Django Angular Setup"
git push -u origin master