I am basing this work on Django for Beginners, but as that book is focused on Linux/Mac I have kept these notes for what I needed to do on my PC. I will keep extending this over the week of Aug 13 (yes, Friday the 13th).
Get Django set up in Virtual Environment
https://codesource.io/setting-up-a-virtual-environment-for-your-django-project/
https://stackoverflow.com/questions/35950740/virtualenv-is-not-recognized-as-an-internal-or-external-command-operable-prog
- pip install virtualenv
- I had to open an administrator cmd prompt and do
- c:\python39\python.exe -m pip install --upgrade pip
For each project you need to create virtualenv and add django
- cd into the directory where you want to create python (e.g., C:\Users\madyor\OneDrive - Microsoft\Desktop\python\django\playground)
- python -m virtualenv .
- .\scripts\activate
- pip install django
- pip install pandas
- run
django-admin startproject
to verify that django is installed.
Create Your Project
- django-admin startproject config . Note: the . at the end places the project in the current directory (instead of creating a new child with that name)
- python manage.py migrate
- python manage.py runserver If this command fails, you may need to do the .\scripts\activate to get into the virtual environment (where django is installed)
- exit gets you out of a virtualenv
Create another app in your project
- python manage.py startapp pages
- Register the app in config/settings (under installed_apps add
'pages',
) - update the views page with the following code
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Page
from django.views.generic import TemplateView
from django.shortcuts import render
class PageListView(ListView):
model=Page
template_name = 'home.html'
class PageDetailView(DetailView):
model=Page
template_name = 'page_detail.html'
class PageCreateView(CreateView):
model = Page
template_name = 'page_new.html'
fields=['title', 'summary', 'description']
class PageUpdateView(UpdateView):
model = Page
template_name = 'page_new.html'
fields=['title', 'summary', 'description']
- create a urls.py within the pages folder
from django.urls import path
from .views import PageDetailView, PageListView, PageCreateView, PageUpdateView
urlpatterns = [
path('page/new/', PageCreateView.as_view(), name='page_new'),
path('page/<int:pk>/', PageDetailView.as_view(), name='page_detail'),
path('page/<int:pk>/edit', PageUpdateView.as_view(), name='page_update'),
path('', PageListView.as_view(), name='home'),
]
- update the config/urls.py to reference the pages.urls (
path('', include('pages.urls')),
and addfrom django.urls.conf import include
)
Initiate Git
- git init
- git status
- git Add -A
- git commit -m "initial commit"
Create a Repo on Github
- git remote add origin https://github.com/dyor/special-octo-journey.git
- git branch -M main
- git push -u origin main
Day 2
Using Templates
- Templates are like views in MVC (and views are like controllers)
- In the config/settings.py, update DIRS to this:
'DIRS': [str(BASE_DIR.joinpath('templates'))],
Day 3
Use a base template
In the pages app, create a
templates
folder and add ahome.html
and abase.html
filefor
base.html
add some code
<a href={% url 'home' %}>Home</a>
{% block content %}
{% endblock content %}
- in the
home.html
file add
{% extends 'base.html' %}
{% block content %}
<h1>Homepage</h1>
{% for page in object_list %}
<div>
<a href="{% url 'page_detail' page.pk %}">{{ page.title }}</a>
</div>
{% endfor %}
{% endblock %}
- Create a detail page in
templates
folder calledpage_detail.html
{% extends 'base.html' %}
{% block content %}
<h1>{{ page.title }}</h1>
{{ page.summary }}
<br/>
<br/>
{{ page.description }}
{% endblock %}
- add model to
admin.py
inadmin
page
from django.contrib import admin
from .models import Page
# Register your models here.
admin.site.register(Page)
Random Commands
python manage.py createsuperuser
python manage.py makemigrations
python manage.py migrate
Top comments (0)