Django + Python 3.4 + WAMP Apache 2.4.23 + Pycharm

Finally worked instance for hosting DEV site code on python and ability to resolve is as «on live» web server just on my laptop.

A lot of different configurations changed but this is one working for me, and maybe can help you also.

Better use EVERYTHING 32 bit for Window, you will suffer if you try to find some needed wheels for python 64bit on Windows 7 64bit.

 

WAMP Apache 2.4.23

Download: wampserver3.0.6_x86_apache2.4.23_mysql5.7.14_php5.6.25-7.0.10 Here

NOTE: Install all needed C++ Redistr. packages which are listed in WAMP install guide!

Download mod_wsgi.so Here or Here (for everythin 32 bit and python 3.4 on Win7 you need: mod_wsgi-py34-VC10.so)

NOTE: Use mod_wsgi version which corresponds your python version! (Above in links you can find more info, just read carefully)

After WAMP install — you don’t need to run full programm (I just did not see any advantages of this and disable wampmysql service)

All you need is here: C:\wamp\bin\apache\apache2.4.23

  1. Place mod_wsgi-py34-VC10.so into «C:\wamp\bin\apache\apache2.4.23\modules«
  2. Import module in «C:\wamp\bin\apache\apache2.4.23\conf\httpd.conf» as
    LoadModule wsgi_module modules/mod_wsgi-py34-VC10.so
    1. OPTIONAL: I’ve changed usual port 80 to 8080 in httpd.conf and httpd-vhosts.conf too
      Listen 0.0.0.0:8080
      Listen [::0]:8080
      <VirtualHost *:8080>
  3. Add your virtual host in «C:\wamp\bin\apache\apache2.4.23\conf\httpd-vhosts.conf»
    NOTE: I will add example lower
  4. Now you can start just clear Apache in «C:\wamp\bin\apache\apache2.4.23\bin\ApacheMonitor.exe«
    This is nice and small UI utility which can Start\Restart\Stop the Apache service
    1. OPTIONAL: you can see some errors if occurs in web UI of WAMP here: http://localhost:8080/
      it helps me to track some typos in httpd-vhosts.conf
    2. Usual apache log is sutiated here: «C:\wamp\logs«
      check it if apache won’t start

Python + Django

Now let’s start our application, we need to make properly configured wsgi.py and settings.py and manage.py for django and let Apache to execute python.

This instruction can be usefull for those who understands the basics of django and apache execution.

If you know how to — then pass, else — read instructions below:

This is my project structure:

D:\PROJECTS\CODE\WEB\SITES\WWW\SMM_TOOLS
+---smm_site
|   |   win_manage.py
|   |
|   +---main_code
|   |   |   main_page.py
|   |   |   urls.py
|   |   |   win_settings.py
|   |   |   wsgi.py
|   |   |   __init__.py
|   |   |
|   +---static
|   |   +---css
|   |   |       bootstrap.css
|   |   |
|   +---templates
|   |   |   main.html

In pycharm if you have Languages & Frameforks > Django — it can make those files for you and start the application, so you just can modily files later.
New project -> Django…

  1. Making wsgi.py
    1. You should point path to site folder
      Example: sys.path.append(«D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site»)
    2. Add DJANGO_SETTINGS_MODULE which is pointed to settings.py file
      Example: os.environ[«DJANGO_SETTINGS_MODULE»] = «main_code.win_settings«
  2. Making manage.py
    1. You should pint DJANGO_SETTINGS_MODULE to site_folder/main_code.settings.py 
      Where «main_code» is a folder where your site .py files stores (application).
      Example: os.environ.setdefault(«DJANGO_SETTINGS_MODULE», «main_code.win_settings«)
  3. Making settings.py
    1. Point to the folder itself
      Example: BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    2. After that if path was set correctly you can just include it further:
      1. STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
      2. STATICFILES_DIRS = (os.path.join(BASE_DIR, ‘/static/admin/’))
      3. etc.
    3. Point to wsgi application: WSGI_APPLICATION = ‘main_code.wsgi.application’

Finally, if you make it in the right way — start Apache and see your site on place.

My debug log with paths:

FROM win_settings.py:
PROJECT_ROOT: D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site\\main_code
BASE DIR: D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site
TEMPLATES: D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site\\templates
Static root: D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site\\static
STATICFILES_DIRS: D:/static/admin/

From apache access log:
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/ HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/custom.css HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/bootstrap.css HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/js/jquery-1.11.2.min.js HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/jquery-1.11.2.min.js HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/js/bootstrap.js HTTP/1.1" 200 4746
127.0.0.1 - - [27/Apr/2017:20:11:03 +0300] "GET /date/bootstrap.js HTTP/1.1" 200 4746

Loading static files log:

But here is one problem:

<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="{{ STATIC_URL }}css/custom.css" rel="stylesheet" type="text/css">

Will resolve as 200 OK, but page sctructure will be broken.

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://smm.trianglesis.org.ua:8080/date/css/bootstrap.css".
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://smm.trianglesis.org.ua:8080/date/css/custom.css".

 But when you use:

<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'css/custom.css' %}" rel="stylesheet" type="text/css">

All styles will be loaded OK

127.0.0.1 - - [27/Apr/2017:20:21:13 +0300] "GET /date/ HTTP/1.1" 200 4412
127.0.0.1 - - [27/Apr/2017:20:21:13 +0300] "GET /static/css/bootstrap.css.map HTTP/1.1" 404 355

 Same situation here:

<script src="{% static 'js/jquery-1.11.2.min.js' %}" type="text/javascript"></script>
<script src="{% static 'js/bootstrap.js' %}" type="text/javascript"></script>

 I don’t know why, but I had some problems with PyCharm with imports: http://trianglesis.org.ua/gjango-pycharm-static-files-windows

PyCharm configs:

To describe last one config better just show you some pics:

For small code checks you can run PyCharm + Django debug server.

For full site work with static files as it should work on WEB server — you can use apache.

You can use it simultaneously, but after code update Apache should be, probably, restarted.

 

Below the RAW code of my conf and py files:

httpd-vhosts.conf

<VirtualHost *:8080>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot c:/wamp/www
  <Directory  "c:/wamp/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
  </Directory>
</VirtualHost>

<VirtualHost *:8080>

    ServerName smm.trianglesis.org.ua
    DocumentRoot "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site"

    Alias /templates/  "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/templates/"
    Alias /static/     "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/static/"
    
    Alias /css/        "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/static/css/"
    Alias /js/         "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/static/js/"
    Alias /fonts/      "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/static/fonts/"

    <Directory "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/static">
        Require all granted
    </Directory>

    <Directory "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site">
        Require all granted
    </Directory>

    WSGIScriptAlias / "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/main_code/wsgi.py"

    <Directory "D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site/main_code">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    ErrorLog "d:/Projects/CODE/WEB/sites/www/smm_tools/www_log/ERROR.log"
    CustomLog "d:/Projects/CODE/WEB/sites/www/smm_tools/www_log/ACCESS.log" common
    ServerSignature On

</VirtualHost>

wsgi.py

import os, sys
from django.core.wsgi import get_wsgi_application

sys.path.append("D:/Projects/CODE/WEB/sites/www/smm_tools/smm_site")

os.environ["DJANGO_SETTINGS_MODULE"] = "main_code.win_settings"

application = get_wsgi_application()

win_manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main_code.win_settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

win_settings.py

import os

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'eeeee'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'datetime',
    'registration',)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'main_code.urls'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'main_code.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'EEE',
        'USER': 'EEEE',
        'PASSWORD': 'EEEE',
        'HOST': 'localhost',
        'PORT': '',
    }
}

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Kiev'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '/static/admin/'),
)
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)


print("=========================================================")
print("PROJECT_ROOT:     "+str(PROJECT_ROOT))
print("BASE DIR:         "+str(BASE_DIR))
print("=========================================================")
print("TEMPLATES:        "+str(TEMPLATES[0]['DIRS'][0]))
# print("WIN TEMPLATES:    D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site\\templates")
print("=========================================================")
print("Static root:      "+str(STATIC_ROOT))
# print("WIN Static root:  D:\\Projects\\CODE\\WEB\\sites\\www\\smm_tools\\smm_site\\static")
print("STATICFILES_DIRS: "+str(STATICFILES_DIRS[0]))

Thaaaaaat’s all folks! 

 

 

trianglesis

Об авторе trianglesis

Александр Брюндтзвельт - гений, филантроп, 100 гривен в кармане. Этот блог - "сток" моих мыслей и заметок. Достаточно одного взгляда на него, чтобы понять, что такой же бардак творится у меня в голове. Если вам этот бардак интересен - милости прошу.
Закладка Постоянная ссылка.