Django for-programmers_2

After we run on the server, we have to create different apps.(Day 5–7)

  1. Create an app
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something — e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

MTV pattern v.s. MVC pattern

M stands for “Model,” the data access layer. This layer contains anything and everything about the data: how to access it, how to validate it, which behaviors it has, and the relationships between the data. 
T stands for “Template,” the presentation layer. This layer contains presentation-related decisions: how something should be displayed on a Web page or other type of document. 
V stands for “View,” the business logic layer. This layer contains the logic that accesses the model and defers to the appropriate template(s). You can think of it as the bridge between models and templates.

python startapp stores

├── stores
│ ├── migrations
│ │ └──
│ ├──
│ ├──
│ ├──
│ ├──
│ └── − Just to make sure python handles this folder as a package. − This file helps you make the app modifiable in the admin interface. − This is where all the application models are stored. − This is where your unit tests are. − This is where your application views are.

add configure on

Get the Project to Know About Your Application
At this stage we have our “myapp” application, now we need to register it with our Django project “myproject”. To do so, update INSTALLED_APPS tuple in the file of your project (add your app name) −


2. Create first view


from django.conf.urls import include, url
from django.contrib import admin
from stores.views import home

urlpatterns = [
url(r'^$', home),
url(r'^admin/', include(,

using app is to create function.


In the stores, we create a template folder and create home.html.

url(r'^$', home, name='home')

using name to refer our link.

<a class="navbar-brand" href="{% url 'home' %}">午餐系統</a>

{% url ‘home’ %} is a template tag .

{% tag_name [ argument ... ] %}

It is and name=’home’.

3. Create a model

Model instance reference

Django model is to Store model

┌───────────────┐           ┌───────────────┐
│ Store │ │ MenuItem │
├───────────────┤ ├───────────────┤
│ name │ │ name │
│ note │ 1 n │ price │
│ menu_items │ <──────── │ store │
└───────────────┘ └───────────────┘

open stores/ and edit

class Store(models.Model):

name = models.CharField(max_length=20)
notes = models.TextField(blank=True, default='')

def __str__(self):

class MenuItem(models.Model):

store = models.ForeignKey('Store', related_name='menu_items')
name = models.CharField(max_length=20)
price = models.IntegerField()

def __str__(self):

Indexes for varchar and text columns

When specifying db_index=True on your model fields, Django typically outputs a single CREATE INDEX statement. However, if the database type for the field is either varchar or text (e.g., used by CharField, FileField, and TextField), then Django will create an additional index that uses an appropriate PostgreSQL operator class for the column. The extra index is necessary to correctly perform look ups that use the LIKE operator in their SQL, as is done with the contains and starts with lookup types.

Field Type:





When you create a model, Django will automatically help it join an AutoField named id — corresponding to the database INTEGER, plus the primary key (and auto increment) property. ForeignKey also corresponds to INTEGER, but when it is used, Django will automatically use the value of the implementation of a SELECT, the ID corresponding to the object out. The ForeignKey must have a parameter indicating the name of the model class pointed to by the FK.

__str__ is to hook an onbject to str.

If you leave the command line, you have to set it again.

set DJANGO_SETTINGS_MODULE=lunch.settings.local
python makemigrations stores

set up database

Migrations for 'stores':
- Create model MenuItem
- Create model Store
- Add field store to menuitem

This means that Django has detected that you have added both the MenuItem and the Store, and the association between them. This information is placed in This file will be placed in the store / migrations directory. Check to see!

python migrate stores
python dbshell
sqlite> .tables
auth_group django_admin_log
auth_group_permissions django_content_type
auth_permission django_migrations
auth_user django_session
auth_user_groups stores_menuitem
auth_user_user_permissions stores_store

control-d to quit

Model field reference

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.