[PYTHON] Django girls-3 workflow

Create a PythonAnywhere account

www.pythonanywhere.com Djangohkp

Creating a new project

django-admin startproject mysite . .Installs Django in the current directory

setting change

編集したファイル:settings.py Change time zone TIME_ZONE = 'Asia/Tokyo'

Language change LANGUAGE_CODE = 'ja'

Add static file path STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Add pythonanywhere.com to host ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']

When DEBUG is set to True and ALLOWED_HOSTS is an empty list, the check is automatically performed for the three hosts ['localhost', '127.0.0.1','[:: 1]']. I will. With this setting, it doesn't include the hostname of PythonAnywhere that we're going to deploy and use.

Database setup

SQlite3 is set by default DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } Execute the following command on the console python manage.py migrate

Start the web server

python manage.py runserver

Check connection at the following address http://127.0.0.1:8000/

Creating a Django model

Create a new application

1.python manage.py startapp blog 2. Add an item to setting.py. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog.apps.BlogConfig', ] 3. Define a blog post model in models.py. Write the following settings from django.conf import settings from django.db import models from django.utils import timezone

class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, 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

Reference site https://docs.djangoproject.com/ja/2.2/ref/models/fields/#field-types

Create a table for the model in the database

python manage.py makemigrations blog

Execution result Migrations for 'blog': blog/migrations/0001_initial.py:

python manage.py migrate blog Execution result Operations to perform: Apply all migrations: blog Running migrations: Applying blog.0001_initial... OK

Register for admin

1.blog/admin.pyの内容を書き換える from django.contrib import admin from .models import Post

admin.site.register(Post) 2. Run python manage.py runserver 3. Access http://127.0.0.1:8000/admin/ 4. Execute python manage.py createsuperuser 5. Register user name etc. 6. Login attempt

~ Omitted ~

Deploy

Python anywhere settings

  1. Launch the Python anywhere Bash console
  2. Enter the following command in PythonAnywhere Bash command-line
pip3.7 install --user pythonanywhere
pa_autoconfigure_django.py --python=3.6 https://github.com/<your-github-username>/my-first-blog.git

Execution result

< Running API sanity checks >
   \
    ~<:>>>>>>>>>
Cloning into '/home/djangohkp/djangohkp.pythonanywhere.com'...
remote: Enumerating objects: 29, done.
remote: Counting objects: 100% (29/29), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 29 (delta 3), reused 29 (delta 3), pack-reused 0
Unpacking objects: 100% (29/29), done.
Checking connectivity... done.
< Creating virtualenv with Python3.6 >
   \
    ~<:>>>>>>>>>
Running virtualenv with interpreter /usr/bin/python3.6
Already using interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/bin/python3.6
Also creating executable in /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/b
in/predeactivate
virtualenvwrapper.user_scripts creating /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/b
in/postdeactivate
virtualenvwrapper.user_scripts creating /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/b
in/preactivate
virtualenvwrapper.user_scripts creating /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/b
in/postactivate
virtualenvwrapper.user_scripts creating /home/djangohkp/.virtualenvs/djangohkp.pythonanywhere.com/bin/get_env_details

  ___________________________________________________________________
/                                                                     \
| Pip installing -r                                                   |
| /home/djangohkp/djangohkp.pythonanywhere.com/requirements.txt (this |
| may take a couple of minutes)                                       |
\                                                                     /
  -------------------------------------------------------------------
   \
    ~<:>>>>>>>>>
Looking in links: /usr/share/pip-wheels
Collecting Django~=2.2.4
  Downloading Django-2.2.11-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 21.4 MB/s 
Processing /usr/share/pip-wheels/pytz-2019.3-py2.py3-none-any.whl
Collecting sqlparse
  Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 973 kB/s 
Installing collected packages: pytz, sqlparse, Django
dSuccessfully installed Django-2.2.11 pytz-2019.3 sqlparse-0.3.1

< Creating web app via API >
   \
    ~<:>>>>>>>>>

< Adding static files mappings for /static/ and /media/ >
   \
    ~<:>>>>>>>>>

< Updating wsgi file at /var/www/djangohkp_pythonanywhere_com_wsgi.py >
   \
    ~<:>>>>>>>>>

< Updating settings.py >
   \
    ~<:>>>>>>>>>
< Running collectstatic >
   \
    ~<:>>>>>>>>>

119 static files copied to '/home/djangohkp/djangohkp.pythonanywhere.com/static'.

< Running migrate database >
   \
    ~<:>>>>>>>>>
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying blog.0001_initial... OK
  Applying sessions.0001_initial... OK

< Reloading djangohkp.pythonanywhere.com via API >
   \
    ~<:>>>>>>>>>

  ____________________________________
/                                      \
| All done!  Your site is now live at  |
| https://djangohkp.pythonanywhere.com |
\                                      /
  ------------------------------------
   \
    ~<:>>>>>>>>>


  ___________________________________________________________________
/                                                                     \
| Starting Bash shell with activated virtualenv in project directory. |
| Press Ctrl+D to exit.                                               |
\                                                                     /
  -------------------------------------------------------------------
   \
    ~<:>>>>>>>>>
  1. Initialize the administrator account Since the database on PythonAnywhere and the database on the local PC are different, register an administrator account here as well. It should be the same as your local account.

  2. Try to access the created web page http://.pythonanywhere.com/admin/

Web development workflow Change locally → Push to DitHub → Web server pulls changes

In the production environment, additional security settings are required in addition to this. Reference page https://docs.djangoproject.com/ja/2.2/howto/deployment/checklist/

Django view

・ About the outline of the view (quoted from the original text) The view is where you write the logic of your app. The view requests information from the model you created earlier and passes it to the template. Templates will be created in the next chapter. Views are just Python functions.

  1. Add to blog / views.py
def post_list(request):
    return render(request, 'blog/post_list.html', {})
  1. Access http://127.0.0.1:8000/ TemplateDoesNotExist at / error is displayed

HTML editing

  1. Create a templates folder in the blog folder and a blog folder in it.
  2. Create post_list.html in blog / template / blog.
  3. Access http://127.0.0.1:8000/
  4. Success if blank html is displayed
  5. Edit html

Push code to GitHub

  1. Run git in your working directory command-line
git status
  1. Make git reflect all changes in the directory
git add --all.
  1. Check upload (all files to upload are displayed in green)
git status
  1. Leave a comment in the change history (comments are also reflected on Github)
git commit -m "Content of comment"
  1. Upload to Github (push)
git push

Pull new code to Python anywhere

  1. In the Python anywhere Bash console, do the following:
cd ~/<your-pythonanywhere-domain>.pythonanywhere.com
git pull
  1. Go to the "web" tab from the Python anywhere home page and reload to update the app.

CSS The changes in the created CSS are not reflected. Verification required

Recommended Posts

Django girls-3 workflow
Django Girls Tutorial Note
Django
Django Girls Tutorial Summary First Half
Django note 4
Django memorandum
Django installation
Django test
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField