I'm going to create a minimal website configuration environment with django.
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.
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')),
]
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>
my_site
├─my_site
│ ├─ __init__.py
│ ├─settings.py
│ ├─templates
│ │ └─index.html
│ ├─urls.py
│ ├─views
│ │ ├─__init__.py
│ │ ├─index.py
│ │ └─urls.py
│ └─wsgi.py
└─manage.py
# 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.
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