Getting to hello, world in Django is kind of a pain. If you find yourself having some trouble getting up and running on your Windows machine and want to get to “hello, world” as quickly as possible, I hope you find my cheat sheet below helpful. This isn’t really tutorial. For that, I suggest going through the official Django Polls App Tutorial. However, if you just want a quick reference for the future, this is the guide for you!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd Desktop | |
$ mkdir HelloDjango | |
$ cd HelloDjango | |
// Create Virtual Environment with Python's venv | |
$ python -m venv env | |
// Activate the Virtual Environment (Windows) | |
$ env\Scripts\activate | |
// Install Django | |
(env) $ python -m pip install Django | |
// Create / Update requirements.txt File | |
(env) $ pip freeze > requirements.txt | |
// Running someone else's Django App? | |
// See below how to install dependencies from requirements.txt file... | |
(env) $ pip install -r requirements.txt | |
// Create Django Project (Project Can Contain Many Apps) | |
(env) $ django-admin startproject hellodjango | |
// Run Django Development Server | |
(env) $ python manage.py runserver | |
// Create Django App | |
(env) $ cd hellodjango | |
(env) $ python manage.py startapp hellodjangoapp | |
// Connect new "App" to Project | |
Open the settings.py file under the hellodjango directory, | |
and add hellodjangoapp.apps.HellodjangoappConfig to INSTALLED_APPS | |
Example: | |
# Application definition | |
INSTALLED_APPS = [ | |
'hellodjangoapp.apps.HellodjangoappConfig', | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
] | |
// Add your first view (hello, world) to hellodjangoapp/views.py | |
from django.http import HttpResponse | |
# Create your views here. | |
def index(request): | |
return HttpResponse("hello, world") | |
// Create your app's urls.py file (will also need to update project urls.py after this) | |
from django.urls import path | |
from . import views | |
urlpatterns = [ | |
path('', views.index, name='index'), | |
] | |
// Now update your project's urls.py file (notice updated import statements) | |
from django.contrib import admin | |
from django.urls import include, path | |
urlpatterns = [ | |
path('', include('hellodjangoapp.urls')), | |
path('admin/', admin.site.urls), | |
] | |
// Run development server (run from hellodjango directory) | |
(env) $ cd hellodjango | |
(env) $ python manage.py runserver | |
// Now visit http://127.0.0.1:8000 in your web browser to see your hello, world in action | |
// NOTE: Apparently Django will create your requirements.txt file for you | |
// the first time you run the development server, so the requirements.txt | |
// code above is likely NOT necessary, consider removing from instructions. |