Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django Blog Project - Development Part 1: Project Creation

Back


Create GitHub Repo

repo


Create Django Project

Clone Repo

git clone https://github.com/simonangel-fong/Django_Blog.git

Create Django Project

cd Django_Blog
py -m venv env # create virtual environment
python.exe -m pip install --upgrade pip # upgrade pip
pip install django
pip list
django-admin startproject django_blog  # Start project
py ./django_blog/manage.py runserver 8000  # Starts a lightweight development web server locally
.vscode/

Feature: Home Page

from django.views.generic.base import TemplateView


# View of home page
class HomeView(TemplateView):
    template_name = "index.html"     # the template of this view

    # define the context data
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # a dictionary representing the context
        context["msg"] = "hellow world"
        return context

from django.contrib import admin
from django.urls import path
from .views import HomeView

urlpatterns = [
    path('', HomeView.as_view(), name="home"),
    path('admin/', admin.site.urls),
]


Static Files


# region Static

# URL to use when referring to static files located in STATIC_ROOT.
STATIC_URL = 'static/'
# defines the additional locations the staticfiles app will traverse
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = Path(BASE_DIR, 'collectstatic')

# endregion

py ./Arguswatcher/manage.py collectstatic

Database

# creating new migrations based on the changes
py ./Arguswatcher/manage.py makemigrations  # No changes detected
# applying migrations.
py ./Arguswatcher/manage.py migrate  #  Apply all migrations: admin, auth, contenttypes, sessions

Test Application locally

home

admin


Output installed packages

pip freeze > requirements.txt

Push Github

git add -A  # Add all file contents to the index.
git log --oneline -4   # Show commit logs
git commit -m "create django project Arguswatcher"  # Record changes to the repository
git push  # Update remote repo


TOP