In this short workshop, we'll be using Django, an open-source web framework written in Python, to develop a simple blogging platform.
Note that below $
denotes what should be entered into the command prompt. Enter what follows it into the macOS Terminal or Windows Command Prompt.
-
If you don't have a recent version of Python installed, first install the Brew package manager, which helps you manage dependencies on macOS.
-
Install necessary dependencies:
$ brew install python && brew install sqlite
- Install
virtualenv
globally via pip, the Python package manager. This tool allows to create virtual environments to help make development more manageable.
$ pip install virtualenv
- Create a new virtualenv named
blog
in a convenient location on your machine:
$ virtualenv blog
- Navigate into the created
blog
folder usingcd blog
and activate the virtualenv:
$ source bin/activate
You'll now see that your shell reflects the change by indicating (blog)
at the beginning of your prompt
- Install Django via pip:
$ pip install Django
- Create a new Django project:
$ django-admin startproject myblog
-
Navigate into the created
myblog
project folder usingcd myblog
-
Set up the database by running the migrate tool:
$ ./manage.py migrate
In your project folder, you'll see a file db.sqlite3
where your SQLite database is located
- Run the development server on port 8000
$ ./manage.py runserver
-
Navigate in your browser to
http://localhost:8000
-
(Optional) Install Visual Studio Code: https://code.visualstudio.com/docs/setup/mac
-
If you don't have a recent version of Python install, first install the Chocolatey package manager, which helps you manage dependencies on Windows.
-
Install necessary dependencies:
$ choco install python sqlite
- Install
virtualenv
globally via pip, the Python package manager. This tool allows to create virtual environments to help make development more manageable.
$ pip install virtualenv
- Create a new virtualenv named
blog
in a convenient location on your machine:
$ virtualenv blog
- Navigate into the created
blog
folder usingcd blog
and activate the virtualenv:
$ Scripts\activate
You'll now see that your shell reflects the change by indicating (blog)
at the beginning of your prompt
- Install Django via pip:
$ pip install Django
- Create a new Django project:
$ django-admin startproject myblog
-
Navigate into the created
myblog
project folder usingcd myblog
-
Set up the database by running the migrate tool:
$ python manage.py migrate
In your project folder, you'll see a file db.sqlite3
where your SQLite database is located
- Run the development server on port 8000
$ python manage.py runserver
-
Navigate in your browser to
http://localhost:8000
-
(Optional) Install Visual Studio Code: https://code.visualstudio.com/docs/setup/windows
- First, let's create a new app:
$ python manage.py startapp posts
- In the newly created
posts/views.py
file, include the following code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world!")
- Create a new file at
posts/urls.py
, and include the following code:
from django.conf.urls import url
from views import index
urlpatterns = [
url(r'^$', index, name='index'),
]
- Now, we need to link up the project and the application urls. Update your
myblog/urls.py
file with the following code:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^posts/', include('posts.urls')),
url(r'^admin/', admin.site.urls),
]
- Now, we're going to create our
Posts
model. Inposts/models.py
, include the following code:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
# timestamps
created = models.DateTimeField(
auto_now_add=True
)
updated = models.DateTimeField(
auto_now=True
)
- Our project needs to be aware of our new model. To do this, we add our application to
myblog/settings.py
. At the bottom ofINSTALLED_APPS
, include the new application's configuration, so it looks like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts.apps.PostsConfig'
]
- Now, we create a new migration to reflect changes in our model:
$ python manage.py makemigrations
- Apply the changes to our database:
$ python manage.py migrate
- Update
posts/views.py
with the following code:
from django.views.generic import ListView
from .models import Post
class PostListView(ListView):
template_name = 'posts/post_list.html'
model = Post
- Create a new template at
posts/templates/posts/post_list.html
:
<h1>My blog</h1>
{% if post_list %}
{% for post in post_list %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<p><small>Posted: {{ post.created }}</small></p>
<hr/>
{% endfor %}
{% else %}
<p>No posts...yet!</p>
{% endif %}
- Update
urlpatterns
inposts/urls.py
, replacing theindex
method we had tested earlier to create "Hello World"
from django.conf.urls import url
from views import PostListView
urlpatterns = [
url(r'^$', PostListView.as_view(), name='post-list'),
]
- First, we need to make the admin system aware of our new model so that we can make changes to it, such as adding, deleting, and editing posts. Add the following code to
posts/admin.py
:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
- Create a new superuser to accesss the backend by following the prompts after running:
$ python manage.py createsuperuser
- Visit your new admin system at
http://localhost:8000/admin
to create new blog posts.
Now you have a basic functioning system! You may want to explore setting up a DetailView
for displaying single posts (we've implemented this in this repository--see posts/views.py
), customizing the admin system, and how you can better style your pages (e.g. using Twitter Bootstrap)