-
Notifications
You must be signed in to change notification settings - Fork 0
Django
https://www.djangoproject.com/download/
$ pip install Django==3.1.7
$ python
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
3.1.7
Upgrade to the latest
$ python -m pip install -U Django
$ python -m django --version
3.2.3
https://www.djangoproject.com/start/
https://docs.djangoproject.com/en/3.2/intro/tutorial01/
UFW Firewall Rules
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04 https://linuxize.com/post/how-to-list-and-delete-ufw-firewall-rules/
$ sudo ufw status verbose
$ sudo ufw enable
$ sudo ufw default deny incoming
$ sudo ufw allow 8899
Go to mysmypoll/settings.py, and add to ALLOWED_HOSTS
ALLOWED_HOSTS = ['xxx.xxx.xxx.xxx', 'localhost', '127.0.0.1']
To create a site
$ pwd
/home/lchen154/workspace/django
$ django-admin startproject mypoll
To run tests
$ python -Wa manage.py test
To run server
# to listen on default port 8000
python manage.py runserver
# to listen on a specific port
python manage.py runserver 8899
# to listen on all available public IPs
python manage.py runserver 0:8899
Internally, go to visit http://127.0.0.1:8899/
Create the "polls" app
$ python manage.py startapp polls
Add the "index" view to polls/views.py
polls/views.py¶
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Create a URLconf in the polls directory, create a file called urls.py.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]And, point the URLconf at the polls.urls module, in mysite/urls.py.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Now, run http://127.0.0.1:8899/polls/
Install MySQL
Install mysqlclient https://pypi.org/project/mysqlclient/
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
pip install mysqlclient
Grant all privileges to the user
mysql> GRANT ALL on *.* TO 'mysqluser1'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Create DB
mysql> create database mypolldb;
Modify 'mypoll/settings.py'
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mypolldb',
'USER': 'mysqluser1',
'PASSWORD': 'testtest1',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
$ python manage.py migrate
In MySQL, you'll see
mysql> show tables;
+----------------------------+
| Tables_in_mypolldb |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
10 rows in set (0.01 sec)
$ python manage.py runserver 0:8899
Add the app config to mypolls/settings.py 'polls.apps.PollsConfig' points to 'polls/apps.py' > 'PollsConfig'
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Store changes of the 'polls' app to the models as a migration.
$ python manage.py makemigrations polls
The migration file is in polls/migrations/0001_initial.py
Use the sqlmigrate command to print out what SQL Django is going to do
$ python manage.py sqlmigrate polls 0001
--
-- Create model Question
--
CREATE TABLE `polls_question` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `question_text` varchar(200) NOT NULL, `pub_date` datetime(6) NOT NULL);
--
-- Create model Choice
--
CREATE TABLE `polls_choice` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `choice_text` varchar(200) NOT NULL, `votes` integer NOT NULL, `question_id` bigint NOT NULL);
ALTER TABLE `polls_choice` ADD CONSTRAINT `polls_choice_question_id_c5b4b260_fk_polls_question_id` FOREIGN KEY (`question_id`) REFERENCES `polls_question` (`id`);
Use python manage.py check to check for any problems in the project without making migrations or touching the database.
Run migrate to create model tables
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0001_initial... OK
The three-step guide to making model changes:
Change your models (in models.py).
Run python manage.py makemigrations to create migrations for those changes
Run python manage.py migrate to apply those changes to the database.