Created
July 25, 2020 05:18
-
-
Save topherPedersen/5c9fa76d8f31b7acb111cf9b0f11b9bc to your computer and use it in GitHub Desktop.
Django Hello, World / Quickstart / Cheatsheet for Windows
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment