{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Django\n", "https://docs.djangoproject.com/en/1.11/intro/tutorial01/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.11.3\n" ] } ], "source": [ "python -m django --version" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": {}, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cd /host/root/data/django_phone" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# startproject" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": {}, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "export project_name=django_phone" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "django-admin startproject $project_name\n", "cd $project_name" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "django_phone manage.py" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": {}, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "curl -s https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore > .gitignore\n", "sed -i \"s/^ALLOWED_HOSTS.*/ALLOWED_HOSTS = ['*']/\" $project_name/settings.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "python manage.py runserver 0:8000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# startapp" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": {}, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "python manage.py startapp contacts" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ".:\r\n", "contacts django_phone\tmanage.py\r\n", "\r\n", "./contacts:\r\n", "__init__.py admin.py apps.py\tmigrations models.py tests.py views.py\r\n", "\r\n", "./contacts/migrations:\r\n", "__init__.py\r\n", "\r\n", "./django_phone:\r\n", "__init__.py __pycache__ settings.py urls.py\twsgi.py\r\n", "\r\n", "./django_phone/__pycache__:\r\n", "__init__.cpython-36.pyc urls.cpython-36.pyc\r\n", "settings.cpython-36.pyc wsgi.cpython-36.pyc" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ls -R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## migrate " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36;1mMigrations for 'contacts':\u001b[0m\r\n", " \u001b[1mcontacts/migrations/0002_contact_sync.py\u001b[0m\r\n", " - Add field sync to contact" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "python manage.py makemigrations contacts" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BEGIN;\r\n", "--\r\n", "-- Create model Contact\r\n", "--\r\n", "CREATE TABLE \"contacts_contact\" (\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT, \"name\" varchar(50) NOT NULL, \"vcard\" text NOT NULL);\r\n", "COMMIT;" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "python manage.py sqlmigrate contacts 0001" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36;1mOperations to perform:\u001b[0m\r\n", "\u001b[1m Apply all migrations: \u001b[0madmin, auth, contacts, contenttypes, sessions\r\n", "\u001b[36;1mRunning migrations:\u001b[0m\r\n", " Applying contacts.0002_contact_sync...\u001b[32;1m OK\u001b[0m" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "python manage.py migrate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## User" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "python manage.py createsuperuser" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Restful\n", "http://www.django-rest-framework.org/tutorial/quickstart/" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Requirement already satisfied: djangorestframework in /opt/conda/lib/python3.6/site-packages" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pip install djangorestframework" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{\r\n", " \"count\": 2,\r\n", " \"next\": null,\r\n", " \"previous\": null,\r\n", " \"results\": [\r\n", " {\r\n", " \"url\": \"http://127.0.0.1:8000/api/contacts/1/\",\r\n", " \"name\": \"n1\",\r\n", " \"vcard\": \"v1\"\r\n", " },\r\n", " {\r\n", " \"url\": \"http://127.0.0.1:8000/api/contacts/2/\",\r\n", " \"name\": \"n2\",\r\n", " \"vcard\": \"v2\"\r\n", " }\r\n", " ]\r\n", "}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "curl -H 'Accept: application/json; indent=4' -u user:password http://127.0.0.1:8000/api/contacts/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## filtering\n", "http://www.django-rest-framework.org/api-guide/filtering/#django-url-filter" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Collecting django-filter\r\n", " Downloading django_filter-1.0.4-py2.py3-none-any.whl (43kB)\r\n", "\u001b[?25l\r", "\u001b[K 23% |███████▋ | 10kB 48kB/s eta 0:00:01\r", "\u001b[K 47% |███████████████▏ | 20kB 90kB/s eta 0:00:01\r", "\u001b[K 71% |██████████████████████▊ | 30kB 134kB/s eta 0:00:01\r", "\u001b[K 94% |██████████████████████████████▎ | 40kB 130kB/s eta 0:00:01\r", "\u001b[K 100% |████████████████████████████████| 51kB 161kB/s \r\n", "\u001b[?25hInstalling collected packages: django-filter\r\n", "Successfully installed django-filter-1.0.4" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pip install django-filter" ] } ], "metadata": { "kernelspec": { "display_name": "MetaKernel Bash", "language": "bash", "name": "metakernel_bash" }, "language_info": { "file_extension": ".sh", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-bash", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 2 }