Following: https://tutorial.djangogirls.org


Python object oriented programming:

Object has 'Properties' and 'Methods'


-Start application in the django project

(env) $ python manage.py startapp app_name


Failures that I encounterd:


-django application start error

1
2
3
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
 
Did you install mysqlclient?
cs


>>

$ sudo pip install mysqlclient


-pip install error

1
2
3
compilation terminated.

    error: command 'gcc' failed with exit status 1
cs


>>

$ yum search python3 | grep devel

$ yum install python36u-devel


-Say to django that I will use 'app_name'

application/settings.py


1
2
3
4
5
6
7
8
9
10
11
# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app_name'
]
cs


-Make a sample model

app_name/migrations/models.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.db import models
from django.utils import timezone
 
 
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
            default=timezone.now)
    published_date = models.DateTimeField(
            blank=True, null=True)
 
    def publish(self):
        self.published_date = timezone.now()
        self.save()
 
    def __str__(self):
        return self.title
cs


-Notice that the new model is ready to database

(env) $ python manage.py makemigrations app_name


-If you met this WARNINGS,

WARNINGS:

?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'

HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode


>>

1
2
3
4
MariaDB [(none)]> set sql_mode='STRICT_TRANS_TABLES'
    -> ;
Query OK, 0 rows affected (0.01 sec)
 
cs


-Apply the model to the database

(env) $ python manage.py migrate


-Create superuser for django admin

(env) $ python manage.py createsuperuser


-Create sample django admin

1
2
3
4
from django.contrib import admin
from .models import Post
 
admin.site.register(Post)
cs


-View admin page: 127.0.0.1:8000/admin


'Python' 카테고리의 다른 글

Flask on apache via mod_wsgi  (0) 2018.12.06
Python 2.6 to 2.7 (or 3.x)  (0) 2018.11.23
Django: 0. Preparing environment  (0) 2018.05.28
파이썬 장단점  (0) 2018.05.24
Set up Python 3 on the CentOS 7  (1) 2016.01.29

+ Recent posts