Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adandan01 committed Oct 3, 2016
0 parents commit f080c79
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Django formset tutorial
==================

This is a simple django app that will demonstrate how to use django formset. I recently wanted to use the formset in my work but i couldn't find any good working example.
I ended doing a lot of trial and error and looked at the django source code. At tne end, I really enjoyed using this feature but wish the documentation would be better now. This is my small attempt to make it better for someone else.

I will use a simple django app called mybook to demostrate the different usages of formset. The model and the scenario is faily simple.

Mybook is a social network app. In the app, you can edit your social profile. One section of the profile is about your family.
You can add as family member as you like. For each family member, you need to specify family member's relationship with you.


example 1
----

Binary file added db.sqlite3
Binary file not shown.
2 changes: 2 additions & 0 deletions export_django_setting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
export DJANGO_SETTINGS_MODULE=mybook.settings
10 changes: 10 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mybook.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Empty file added mybook/__init__.py
Empty file.
Binary file added mybook/__init__.pyc
Binary file not shown.
123 changes: 123 additions & 0 deletions mybook/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Django settings for mybook project.
Generated by 'django-admin startproject' using Django 1.9.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'w4)03ckn%^uu2^p(gv(l)ykl2jf9cz$=fw-mzypzzn-sz*cgs9'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myprofile',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mybook.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'mybook.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Binary file added mybook/settings.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions mybook/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""mybook URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
]
Binary file added mybook/urls.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions mybook/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for mybook project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mybook.settings")

application = get_wsgi_application()
Binary file added mybook/wsgi.pyc
Binary file not shown.
Empty file added myprofile/__init__.py
Empty file.
Binary file added myprofile/__init__.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions myprofile/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

from .models import Profile, FamilyMember

admin.site.register(Profile)
admin.site.register(FamilyMember)
7 changes: 7 additions & 0 deletions myprofile/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import unicode_literals

from django.apps import AppConfig


class MyprofileConfig(AppConfig):
name = 'myprofile'
Empty file added myprofile/forms.py
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions myprofile/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import unicode_literals


from django.db import models
from django.utils import timezone


class Profile(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
created_date = models.DateTimeField(default=timezone.now)


class FamilyMember(models.Model):
profile = models.ForeignKey(Profile)
member_name = models.CharField(max_length=100)
relationship_name = models.CharField(max_length=100)

Binary file added myprofile/models.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions myprofile/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions myprofile/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit f080c79

Please sign in to comment.