[PYTHON] Minimal website environment with django

I'm going to create a minimal website configuration environment with django.

environment

django environment construction

For the time being, install virtualenvwrapper etc. and prepare the environment.

# sudo pip install virtualenvwrapper
# mkvirtualenv --python=/path/to/python/2.7.11/bin/python my_site
# pip install django==1.9
# pip install mysql-python
# django-admin startproject my_site
# cd my_site
# mysql -u root
> CREATE DATABASE my_site;
Query OK, 1 row affected (0.00 sec)
> exit

Update the contents of settings. Change the contents of DATABASES to mysql Change LANGUAGE_CODE to ja.

my_site/my_site/settings.py


# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILE_DIR = os.path.dirname(os.path.abspath(__file__))  #Hierarchy where settings exist
BASE_DIR = os.path.dirname(FILE_DIR)  #Build paths hierarchy

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_site',
        'USER': 'root',
        'PASSWORD': '',
    },
}

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Japan'

Create a user for django's DB environment and admin admin screen.

# cd my_site
# python manage.py migrate
# python manage.py createsuperuser
Username (leave blank to use 'yuji.kanazawa'): admin
Email address: [email protected]
Password: ******
Password (again): ******
# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
April 06, 2016 - 09:21:55
Django version 1.9, using settings 'my_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Please connect to http://127.0.0.1:8000/admin/. Enter the username and password set in createsuperuser to log in and you'll see django The management screen provided by default is displayed.

By the way, without changing LANGUAGE_CODE changed in settings If you leave it as "en-us", the English management screen will be displayed.

view creation

Create a "my_site / my_site / views" folder. The structure in the corresponding folder is as follows. The contents of \ _ \ _ init \ _ \ _. Py are empty and there is no problem.

views
├─__init__.py
├─index.py
└─urls.py

my_site/my_site/views/index.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from django.views.generic import TemplateView
from django.utils.translation import ugettext as _


class IndexView(TemplateView):
    template_name = "index.html"

    def get(self, request, *args, **kwargs):
        context = {
            "title": _("TOP page"),
            "text": _("Hello World"),
        }
        return self.render_to_response(context)

my_site/my_site/views/urls.py


# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from django.conf.urls import url
from .index import IndexView


urlpatterns = [
    url(r'^$', IndexView.as_view(), name='api-index'),
]

Add views to INSTALLED_APPS in settings with the above support

my_site/my_site/settings.py


INSTALLED_APPS = [
    〜,
]

INSTALLED_APPS += [
    'my_site.views',
]

After that, register the views urls in urls.py

my_site/my_site/urls.py


from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^apis/', include('my_site.views.urls')),
]

Template creation

Create a "my_site / my_site / templates" folder.

views
└─index.html

my_site/my_site/templates/index.html


{% load i18n %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>{% trans title %}</title>
</head>
<body>
{% trans text %}
</body>
</html>

Folder structure

my_site
├─my_site
│ ├─ __init__.py
│ ├─settings.py
│ ├─templates
│ │ └─index.html
│ ├─urls.py
│ ├─views
│ │ ├─__init__.py
│ │ ├─index.py
│ │ └─urls.py
│ └─wsgi.py
└─manage.py

Server operation check

# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
April 08, 2016 - 14:04:01
Django version 1.9, using settings 'my_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

http://127.0.0.1:8000/views/ When you connect to, the following page will be displayed.

スクリーンショット 2016-04-08 14.50.54.png

Git Hub The above minimum configuration environment is available here. If you like, try cloning.

In the future, I would like to create a model so that data can be flowed to mysql. https://github.com/yu-sa/my_site

Recommended Posts

Minimal website environment with django
Django environment construction
Internationalization with django
django environment construction
CRUD with Django
Build Django + NGINX + PostgreSQL development environment with Docker
Build the fastest Django development environment with docker-compose
Start Django in a virtual environment with Pipenv
[Python] Build a Django development environment with Docker
Build a Django environment with Vagrant in 5 minutes
Create Nginx + uWSGI + Python (Django) environment with docker
Build a Django development environment with Doker Toolbox
Quickly build a Python Django environment with IntelliJ
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Django project environment construction
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
File upload with django
Python environment with docker-compose
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
[Memo] Django development environment
Virtual environment with Python 3.6
Start today with Django
Getting Started with Django 2
Web application made with Python3.4 + Django (Part.1 Environment construction)
Remote debug Django environment created with docker-compose with VS Code
Build a development environment with Poetry Django Docker Pycharm
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django environment for Win10 (with virtual space)
Try running python in a Django environment created with pipenv
Easy tox environment with Jenkins
Get started with Django! ~ Tutorial ⑤ ~
Create an environment with virtualenv
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Install Python environment with Anaconda
Create an API with Django
Manage python environment with virtualenv
[Memo] Build a development environment for Django + Nuxt.js with Docker
Django development environment construction memo
Django Getting Started: 1_Environment Building
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Recommendations for django, wagtail ~ Why develop a website with python ~
Python3 + Django ~ Mac ~ with Apache
venv environment with windows powershell
Getting Started with Python Django (1)
Create a homepage with django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Build python3 environment with ubuntu 16.04
ML environment construction with Miniconda
Web application creation with Django