├── Django-MVC.potx ├── app ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ ├── settings.cpython-37.pyc │ ├── urls.cpython-37.pyc │ └── wsgi.cpython-37.pyc ├── settings.py ├── urls.py └── wsgi.py ├── db.sqlite3 ├── installation.md ├── manage.py └── users ├── __init__.py ├── __pycache__ ├── __init__.cpython-37.pyc ├── admin.cpython-37.pyc ├── apps.cpython-37.pyc ├── forms.cpython-37.pyc ├── models.cpython-37.pyc ├── urls.cpython-37.pyc └── views.cpython-37.pyc ├── admin.py ├── apps.py ├── forms.py ├── migrations ├── 0001_initial.py ├── 0002_auto_20160918_2037.py ├── 0003_auto_20160918_2224.py ├── 0004_auto_20180823_1845.py ├── __init__.py └── __pycache__ │ ├── 0001_initial.cpython-37.pyc │ ├── 0002_auto_20160918_2037.cpython-37.pyc │ ├── 0003_auto_20160918_2224.cpython-37.pyc │ ├── 0004_auto_20180823_1845.cpython-37.pyc │ └── __init__.cpython-37.pyc ├── models.py ├── templates └── users │ ├── add.html │ ├── base.html │ ├── detail.html │ ├── index.html │ └── testTemplate.html ├── tests.py ├── urls.py └── views.py /Django-MVC.potx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/Django-MVC.potx -------------------------------------------------------------------------------- /app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/app/__init__.py -------------------------------------------------------------------------------- /app/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/app/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /app/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/app/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /app/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/app/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /app/__pycache__/wsgi.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/app/__pycache__/wsgi.cpython-37.pyc -------------------------------------------------------------------------------- /app/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for app project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.10.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.10/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.10/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '8+x+0-om)4(2*y4xyqvv)bm^l07kry+x%mg@=&r@+tk$(yzumk' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'users.apps.UsersConfig', 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'app.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'app.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/1.10/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/1.10/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/1.10/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /app/urls.py: -------------------------------------------------------------------------------- 1 | """app URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.10/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 | Including another URL conf 13 | 1. Import the include() function: from django.conf.urls import url, include 14 | 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 15 | """ 16 | from django.conf.urls import include, url 17 | from django.contrib import admin 18 | 19 | app_name = 'users' 20 | urlpatterns = [ 21 | url(r'^admin/', admin.site.urls), 22 | url(r'', include('users.urls')), 23 | ] 24 | -------------------------------------------------------------------------------- /app/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for app project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/db.sqlite3 -------------------------------------------------------------------------------- /installation.md: -------------------------------------------------------------------------------- 1 | # Python Installation & Django Workflow 2 | 3 | ### Installing Python 4 | 5 | Installing Python on Windows is easy. Just find the Windows Executable Installer on the [Python downloads page](https://www.python.org/downloads/release/python-352/), run it, and you're good to go. When the Installation Wizard pops up, be sure to check the `Add python to PATH` option, and at the end of install `allow python to bypass PATH limit`. This will do any manual work involved in adding Python to your PATH automatically. 6 | 7 | Mac users should install [Homebrew](http://brew.sh), and then run `brew install python3`. 8 | 9 | If you're on Linux, you're probably already running Python 3.4.x+. Users of Ubuntu 12.10+ already have Python 3.4.x: Just use `python3` and `pip3` instead of `python` and `pip`. Users of other distributions presumably know what they're doing. 10 | 11 | ### Installing Virtualenv 12 | 13 | You _can_ install Django globally. But you shouldn't. It's better to maintain the environments of projects that rely on particular versions of packages separately. In Python-land, we use [virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/) for this, and our package manager is called [pip](https://pypi.python.org/pypi/pip). 14 | 15 | Virtualenv effectively allows us to create an isolated environment in which we can use a _specific_ Python version and install _specific_ versions of the packages we want. What you do elsewhere won't interfere at all with this isolated environment. 16 | 17 | We'll be using Django 1.10.x, which requires Python 3.4.x+. Since most of you will have both Python 2.7 _and_ 3.4/5 on your system, you need to make sure you're using the right version of pip, since there's one for each. 18 | 19 | First, run: `pip --version`. This will print the version of pip you have, and the version of Python it's using. 20 | 21 | ![pip --version output.](../Images/0-pip-version.png) 22 | 23 | _pip --version output._ 24 | 25 | If you see `Python 3.4.3` or higher to the right, keep using just use the `pip` command. 26 | 27 | If you see `Python 2.7`, as I do, try again with `pip3`. 28 | 29 | ![pip3 --version output.](../Images/0-pip3-version.png) 30 | 31 | _pip3 --version output._ 32 | 33 | If that works, use `pip3` instead. If it doesn't, Slack a TA or instructor—it should. 34 | 35 | Next, we'll install virtualenv. This is a package you _do_ want to install globally, since it's an environment manager. 36 | 37 | Run: 38 | 39 | Open `GitBash` as Adminstrator if using windows. Use `sudo` command on Mac. I.E `sudo pip install virtualenv --user`. 40 | 41 | # Or pip3 install virtualenv --user, if appropriate. 42 | pip install virtualenv --user 43 | 44 | That should install virtualenv globally. 45 | 46 | #### Using Virtualenv 47 | 48 | You'll mostly be doing three things with virtualenv: **Creating** virtual environments; **sourcing** them; and **deactivating** them. It's all quite straightforward. 49 | 50 | ##### Creating a virtualenv 51 | 52 | To create a new virtualenvironment, run: 53 | 54 | virtualenv $ENVIRONMENT_NAME 55 | 56 | ... Where $ENVIRONMENT_NAME can be anything. I generally use `virtualenv env`. 57 | 58 | Note that this will use your system's _default_ Python as the version for the virtual environment. If typing `python --version` prints `Python 3.4.x` or higher, this is fine. If you have to use `pip3` or `python3` explicitly, though, you may need to tell virtualenv to use Python 3 explicitly. 59 | 60 | There are two steps. 61 | 62 | 1. Figure out where your Python 3 installation lives. Write: `which python3`. I'll call the output of this command $PYTHON_3. 63 | 64 | 2. Run: `virtualenv -p $PYTHON_3 $ENVIRONMENT_NAME`, where $PYTHON_3 is the path to your Python 3 installation. 65 | 66 | ![Output of which python3.](../Images/0-which-output.png) 67 | 68 | _Output of which python3._ 69 | 70 | ![Creating a virtualenv with Python 3.](../Images/0-virtualenv-py3.png) 71 | 72 | _Creating a virtualenv with Python 3._ 73 | 74 | ##### Sourcing a virtualenv 75 | 76 | To actually _use_ your sandbox, you need to "source" the virtualenv. This is straightforward: 77 | 78 | For Windows, run: 79 | `source $ENVIRONMENT/Scripts/activate`. 80 | For Mac, run 81 | `source $ENVIRONMENT/bin/activate`. 82 | 83 | Again, I usually use `env` as my $ENVIRONMENT_NAME, so I write: 84 | 85 | source env/Scripts/activate 86 | 87 | or 88 | 89 | source env/Scripts/activate 90 | 91 | ![Sourcing a virtualenv.](../Images/0-sourcing-env.png) 92 | 93 | _Sourcing a virtualenv._ 94 | 95 | You'll notice an `(env)` marker pop up to the left of your command prompt. This simply indicates that you're running in a virtualenv, not your normal system environment. 96 | 97 | At this point, you can install packages with pip just like you normally would. The only difference is that, if you created your virtualenv with Python 3, you can just write `pip` instead of `pip3`. 98 | 99 | For example, to install the version of Django we'll use this week, run: 100 | 101 | pip install Django==1.10.1 102 | 103 | This installs Django 1.10.1, but _only_ in your sandboxed virtual environment. Once you deactivate it, the rest of your system won't know anything about it. 104 | 105 | ##### Deactivating 106 | 107 | To exit your virtualenv sandbox and return to your normal system environment, just use `deactivate`. 108 | 109 | ![Deactivating a virtualenv.](../Images/0-deactivating.png) 110 | 111 | _Deactivating a virtualenv._ 112 | 113 | ### Workflow 114 | 115 | Everyone settles on their own workflows, but the following cycle is common in Django. 116 | 117 | - Write unit tests for your models. 118 | 119 | - Define your models. 120 | 121 | - Migrate your database. 122 | 123 | - Write functional tests for your user stories. 124 | 125 | - Define views corresponding to your models. 126 | 127 | - Wire your URLs to respond to the views. 128 | 129 | - Write templates to "decorate" your views. 130 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /users/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__init__.py -------------------------------------------------------------------------------- /users/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/admin.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/admin.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/apps.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/apps.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/forms.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/forms.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/models.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/models.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /users/__pycache__/views.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/__pycache__/views.cpython-37.pyc -------------------------------------------------------------------------------- /users/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /users/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class UsersConfig(AppConfig): 5 | name = 'users' 6 | -------------------------------------------------------------------------------- /users/forms.py: -------------------------------------------------------------------------------- 1 | from django import forms 2 | 3 | class UserForm(forms.Form): 4 | 5 | first_name = forms.CharField(label='First Name', max_length=256) 6 | 7 | last_name = forms.CharField(label='Last Name', max_length=256) 8 | 9 | email = forms.EmailField(label='Email', max_length=256) 10 | 11 | age = forms.IntegerField(label='Age', min_value=18) 12 | -------------------------------------------------------------------------------- /users/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.10.1 on 2016-09-17 02:42 3 | from __future__ import unicode_literals 4 | 5 | from django.db import migrations, models 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | initial = True 11 | 12 | dependencies = [ 13 | ] 14 | 15 | operations = [ 16 | migrations.CreateModel( 17 | name='User', 18 | fields=[ 19 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 20 | ('first_name', models.CharField(max_length=256)), 21 | ('last_name', models.CharField(max_length=256)), 22 | ('email', models.CharField(max_length=256)), 23 | ('age', models.IntegerField(blank=True, null=True)), 24 | ], 25 | ), 26 | ] 27 | -------------------------------------------------------------------------------- /users/migrations/0002_auto_20160918_2037.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.10.1 on 2016-09-18 20:37 3 | from __future__ import unicode_literals 4 | 5 | import datetime 6 | from django.db import migrations, models 7 | from django.utils.timezone import utc 8 | 9 | 10 | class Migration(migrations.Migration): 11 | 12 | dependencies = [ 13 | ('users', '0001_initial'), 14 | ] 15 | 16 | operations = [ 17 | migrations.AddField( 18 | model_name='user', 19 | name='created', 20 | field=models.DateTimeField(default=datetime.datetime(2016, 9, 18, 20, 37, 23, 328233, tzinfo=utc)), 21 | ), 22 | migrations.AddField( 23 | model_name='user', 24 | name='modified', 25 | field=models.DateTimeField(default=datetime.datetime(2016, 9, 18, 20, 37, 23, 328233, tzinfo=utc)), 26 | ), 27 | ] 28 | -------------------------------------------------------------------------------- /users/migrations/0003_auto_20160918_2224.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.10.1 on 2016-09-18 22:24 3 | from __future__ import unicode_literals 4 | 5 | import datetime 6 | from django.db import migrations, models 7 | from django.utils.timezone import utc 8 | 9 | 10 | class Migration(migrations.Migration): 11 | 12 | dependencies = [ 13 | ('users', '0002_auto_20160918_2037'), 14 | ] 15 | 16 | operations = [ 17 | migrations.AlterField( 18 | model_name='user', 19 | name='created', 20 | field=models.DateTimeField(default=datetime.datetime(2016, 9, 18, 22, 24, 44, 983184, tzinfo=utc)), 21 | ), 22 | migrations.AlterField( 23 | model_name='user', 24 | name='email', 25 | field=models.EmailField(max_length=256), 26 | ), 27 | migrations.AlterField( 28 | model_name='user', 29 | name='modified', 30 | field=models.DateTimeField(default=datetime.datetime(2016, 9, 18, 22, 24, 44, 983184, tzinfo=utc)), 31 | ), 32 | ] 33 | -------------------------------------------------------------------------------- /users/migrations/0004_auto_20180823_1845.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by Django 1.10.1 on 2018-08-23 18:45 3 | from __future__ import unicode_literals 4 | 5 | import datetime 6 | from django.db import migrations, models 7 | from django.utils.timezone import utc 8 | 9 | 10 | class Migration(migrations.Migration): 11 | 12 | dependencies = [ 13 | ('users', '0003_auto_20160918_2224'), 14 | ] 15 | 16 | operations = [ 17 | migrations.AlterField( 18 | model_name='user', 19 | name='created', 20 | field=models.DateTimeField(default=datetime.datetime(2018, 8, 23, 18, 45, 32, 382881, tzinfo=utc)), 21 | ), 22 | migrations.AlterField( 23 | model_name='user', 24 | name='modified', 25 | field=models.DateTimeField(default=datetime.datetime(2018, 8, 23, 18, 45, 32, 383238, tzinfo=utc)), 26 | ), 27 | ] 28 | -------------------------------------------------------------------------------- /users/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__init__.py -------------------------------------------------------------------------------- /users/migrations/__pycache__/0001_initial.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__pycache__/0001_initial.cpython-37.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/0002_auto_20160918_2037.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__pycache__/0002_auto_20160918_2037.cpython-37.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/0003_auto_20160918_2224.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__pycache__/0003_auto_20160918_2224.cpython-37.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/0004_auto_20180823_1845.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__pycache__/0004_auto_20180823_1845.cpython-37.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OSP123/basic-django/becb47cc1c56e227b857b3671dca3eb80bae35a5/users/migrations/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /users/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | from django.utils import timezone 4 | 5 | # Create your models here. 6 | class User(models.Model): 7 | 8 | created = models.DateTimeField(default=timezone.now()) 9 | 10 | modified = models.DateTimeField(default=timezone.now()) 11 | 12 | first_name = models.CharField(max_length=256, blank=False, null=False) 13 | 14 | last_name = models.CharField(max_length=256, blank=False, null=False) 15 | 16 | email = models.EmailField(max_length=256, blank=False, null=False) 17 | 18 | age = models.IntegerField(blank=True, null=True) 19 | 20 | def __str__(self): 21 | """ Sensible string representation of a user.""" 22 | return "{0} {1} | {2}".format(self.first_name, self.last_name, 23 | self.email) 24 | 25 | def save(self, *args, **kwargs): 26 | """ Add created_at and updated_at timestamps. """ 27 | if not self.id: 28 | self.created = timezone.now() 29 | 30 | self.modified = timezone.now() 31 | 32 | return super(User, self).save(*args, **kwargs) 33 | 34 | -------------------------------------------------------------------------------- /users/templates/users/add.html: -------------------------------------------------------------------------------- 1 | {% extends 'users/base.html' %} 2 | 3 | {% block title %} Add {% endblock %} 4 | 5 | {% block content %} 6 | 7 |

Add a User.

8 | 9 |
10 | 11 |
12 | 13 | {% csrf_token %} 14 | 15 | {% for field in form %} 16 |
17 | {{ field.errors }} 18 | 19 | {{ field.label_tag }} {{ field }} 20 |
21 | {% endfor %} 22 | 23 | 24 | 25 |
26 | 27 | {% endblock %} 28 | -------------------------------------------------------------------------------- /users/templates/users/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | {% block title %}{% endblock %} 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | {% block content %} {% endblock %} 17 |
18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /users/templates/users/detail.html: -------------------------------------------------------------------------------- 1 | {% extends 'users/base.html' %} 2 | 3 | {% block title %} Detail {% endblock %} 4 | 5 | {% block content %} 6 | 7 |

Welcome, {{ user.first_name }}.

8 | 9 |
10 | 11 |
Profile Information
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
First Name{{ user.first_name }}
Last Name{{ user.last_name }}
Email{{ user.email }}
30 | 31 | {% endblock %} 32 | -------------------------------------------------------------------------------- /users/templates/users/index.html: -------------------------------------------------------------------------------- 1 | {% extends 'users/base.html' %} 2 | 3 | {% block title %} Index {% endblock %} 4 | 5 | {% block content %} 6 | 7 | {% if name %} 8 |

Hello, {{ name }}!

9 | {% else %} 10 |

Hello, Stranger!

11 | {% endif %} 12 | 13 | {% if user_list %} 14 | 19 | {% else %} 20 |

No users yet.

21 | {% endif%} 22 | 23 | {% endblock %} 24 | -------------------------------------------------------------------------------- /users/templates/users/testTemplate.html: -------------------------------------------------------------------------------- 1 | {% extends 'users/base.html' %} 2 | 3 | {% block title %} testTemplate {% endblock %} 4 | {% block content %} 5 |

Hey, I'm some template content

6 | {% if name %} 7 |

Hello, {{ name }}!

8 | {% else %} 9 |

Hello, Stranger!

10 | {% endif %} 11 | {% endblock %} -------------------------------------------------------------------------------- /users/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /users/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import url 2 | 3 | # This is importing the controller 4 | from . import views 5 | 6 | app_name = 'users' 7 | urlpatterns = [ 8 | url(r'^index/$', views.UserListView.as_view(), name='index'), 9 | 10 | url(r'^detail/(?P[0-9]+)/$', 11 | views.UserDetailView.as_view(), name='detail'), 12 | 13 | url(r'^add/$', views.add, name='add'), 14 | 15 | url(r'^test/$', views.test, name='test'), 16 | 17 | url(r'^testTemplate/$', views.testTemplate, name='testTemplate'), 18 | ] 19 | -------------------------------------------------------------------------------- /users/views.py: -------------------------------------------------------------------------------- 1 | from django.http import Http404, HttpResponse 2 | 3 | from django.shortcuts import redirect, render 4 | 5 | from django.views.generic import DetailView, ListView 6 | 7 | from .forms import UserForm 8 | 9 | from .models import User 10 | 11 | def test(request): 12 | response = """ 13 |

Yo, this works?

14 |

Crazy, I know

15 | """ 16 | return HttpResponse(response) 17 | 18 | def testTemplate(request): 19 | return render(request, 'users/testTemplate.html', { "name": "Mr. Whiskers" }) 20 | 21 | class UserListView(ListView): 22 | model = User 23 | template_name = 'users/index.html' 24 | 25 | def get_context_data(self, **kwargs): 26 | context = super(UserListView, self).get_context_data(**kwargs) 27 | 28 | # In real life we'd retrieve this from the session. 29 | context['name'] = 'Adonis' 30 | 31 | return context 32 | 33 | class UserDetailView(DetailView): 34 | model = User 35 | template_name = 'users/detail.html' 36 | 37 | def add(request): 38 | 39 | if request.method == 'POST': 40 | 41 | form = UserForm(request.POST) 42 | 43 | if form.is_valid(): 44 | # Create and save directly. 45 | 46 | User( 47 | first_name=form.cleaned_data['first_name'], 48 | last_name=form.cleaned_data['last_name'], 49 | email=form.cleaned_data['email'], 50 | age=form.cleaned_data['age']).save() 51 | 52 | return redirect('users:index') 53 | 54 | else: 55 | # Render form with errors. 56 | return render(request, 'users/add.html', { 'form' : form }) 57 | 58 | else: 59 | # If the user sends a GET request... 60 | 61 | context = { 'header' : 'GET', 'form' : UserForm() } 62 | 63 | return render(request, 'users/add.html', context) 64 | --------------------------------------------------------------------------------