In this post, I will try to follow
Django 1.9 tutorial on Windows Server 2008 32bit with Apache server 2.4. Python version is 3.4.4.
Get updated pip
C:\download>python get-pip.py
Collecting pip
Downloading pip-8.1.2-py2.py3-none-any.whl (1.2MB)
100% |################################| 1.2MB 177kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.1
Uninstalling pip-8.1.1:
Successfully uninstalled pip-8.1.1
Successfully installed pip-8.1.2
Install virtualenv - but didn't used it in here and not strictly necessary
C:\download>python -m pip install virtualenv
Collecting virtualenv
Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
100% |################################| 1.8MB 84kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.2
Install Django
C:\download>python -m pip install Django
Collecting Django
Downloading Django-1.9.7-py2.py3-none-any.whl (6.6MB)
100% |################################| 6.6MB 27kB/s
Installing collected packages: Django
Successfully installed Django-1.9.7
Check Django version
>>> import django
>>> print(django.get_version())
1.9.7
>>>
Run Django admin
C:\>python C:\WinPython-32bit-3.4.4.2\python-3.4.4\Lib\site-packages\django\bin\django-admin.py startproject mysite
Modify C:\mysite\mysite\wsgi.py.
import os, sys
from django.core.wsgi import get_wsgi_application
sys.path.append('C:\mysite')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
Modify C:\Apache24\conf\httpd.conf.
WSGIScriptAlias /mysite "C:/mysite/mysite/wsgi.py"
WSGIPythonPath "C:/mysite"
<Directory "C:/mysite/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
You should see below when accessing mysite. Refer previous post for connection port 81.
Start polls app.
C:\mysite>python manage.py startapp polls
Modify C:\mysite\polls\views.py.
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Modify C:\mysite\polls\urls.py.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Modify C:\mysite\mysite\urls.py.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Now you should see index page.
Run migrate
C:\mysite>python manage.py migrate
Operations to perform:
Apply all migrations: sessions, admin, auth, contenttypes
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
...
Modify C:\mysite\polls\models.py.
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
Modify C:\mysite\mysite\settings.py.
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
...
]
Run commands to generate prepare database.
C:\mysite>python manage.py makemigrations polls
Migrations for 'polls':
0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
C:\mysite>python manage.py sqlmigrate polls 0001
BEGIN;
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "c
hoice_text" varchar(200) NOT NULL, "votes" integer NOT NULL);
....
COMMIT;
C:\mysite>python manage.py migrate
Operations to perform:
Apply all migrations: polls, admin, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying polls.0001_initial... OK
Create admin account
C:\mysite>python manage.py createsuperuser
Username (leave blank to use 'administrator'): sungwoo
Email address: the.sungwoo@gmail.com
Password:
Password (again):
Superuser created successfully.
Now you should see admin page but static files - CSS and javascript - are not in place.
Modify C:\mysite\mysite\settings.py.
STATIC_URL = '/static/'
STATIC_ROOT = "c:/mysite/static"
Run collectstatic
C:\mysite>python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings:
c:\mysite\static
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\lib\site-packages\django\contri
b\admin\static\admin\css\base.css'
...
Copying 'C:\WinPython-32bit-3.4.4.2\python-3.4.4\lib\site-packages\django\contri
b\admin\static\admin\js\vendor\xregexp\xregexp.min.js'
56 static files copied to 'c:\mysite\static'.
Modify C:\Apache24\conf\httpd.conf.
Alias /media/ "C:/mysite/media/"
Alias /static/ "C:/mysite/static/"
<Directory "C:/mysite/static">
Require all granted
</Directory>
<Directory "C:/mysite/media">
Require all granted
</Directory>
WSGIScriptAlias /mysite "C:/mysite/mysite/wsgi.py"
WSGIPythonPath "C:/mysite"
<Directory "C:/mysite/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Then fancy painted admin page should come up.