Anda di halaman 1dari 13

Django Setup Guide

Version 1.8 - Python 2.7.5

Douglas J. Hajj
0006.0018.2016

Table of Contents
Introduction Install.............................................................4

Python.......................................................................................4
Pip............................................................................................................. 4
virtualenvwrapper..................................................................................... 4
shell_script................................................................................................ 4
Virtual Environments..................................................................4

Chapter One Setup.............................................................5

Django.......................................................................................5
Project....................................................................................................... 5
IDE.............................................................................................5
Pycharm.................................................................................................... 5
Database....................................................................................5
Migrate...................................................................................................... 5
run_server................................................................................................. 5
Install GIT...................................................................................5
Git configuration........................................................................................ 5
Create repository........................................................................6
Collect and add python/pycharm gitignore...............................................6
Add configuration and gitignore chnges to git..........................................6
Check git status......................................................................................... 6
Commit to git.............................................................................6

Chapter Two Templates, Views, & URLs................................7

App............................................................................................7
Add to git................................................................................................... 7
project settings.py..................................................................................... 7
Views.py....................................................................................7
Templates Dir............................................................................................ 7
templates.html.......................................................................................... 7
Urls.py.......................................................................................7
appname views.py.................................................................................... 7
appname.html........................................................................................... 8
appname urls............................................................................................ 8
Refactor.....................................................................................8
Project URLs.............................................................................................. 8
Commit to git............................................................................................ 8

Chapter Three Models, Databases, Migrations and Admin.....9

appname Models.py..................................................................................9
Migrations..................................................................................9
Initial Migration......................................................................................... 9
ShowTables................................................................................................ 9
Migrate...................................................................................................... 9
Create SuperUser.......................................................................9
appname admin.py................................................................................... 9
appname views.py modification..............................................................10

appname.html modification....................................................................10
Add migrations to git................................................................10

Chapter Four Creating User Authentication........................11

Checking out third party apps....................................................11


Install django-registration.......................................................................11
Django requirements............................................................................... 11
Settings.py.............................................................................................. 11

Chapter - Title....................................................................11

Python.....................................................................................11
SubPython............................................................................................... 11

Introduction Install
This chapter outlines the installation and setup of a simple
django project. Python 2.7.5 is used to keep the initial
documentation concurrent with the instructors code. Python 3
should be used with all future projects.
Python
$python V
Check the python version. At this time, the python version is
2.7.5.
Pip
$sudo easy_install pip
Python package index install. Script found online at
https://bootstrap.pypa.io/get-pip.py . Download script and run
$python get-pip.py Do not install on system python.
virtualenvwrapper
$pip install virtualenvwrapper
Install the virtual environment wrapper.
shell_script
$source /usr/bin/virtualenvwrapper.sh
Install to bash profile following the instructions at this link.
https://virtualenvwrapper.readthedocs.io/en/latest/install.html
Virtual Environments
$mkvirtualenv environmentname-django
Crates the virtual environment and gives it a name. django is
added to show the engine association. To enter an environment
$workon envname. To exit the current environment $deactivate

Chapter One Setup


This chapter outlines the installation and setup of a simple
django project. Python 2.7.5 is used to keep the initial
documentation concurrent with the instructors code. Python 3
should be used with all future projects.
Django
$pip install django==1.8
Install django into the newly created virtual environment. Django
1.8 used to stay concurrent with instructors code.
Project
$django-admin startproject projectname
This command creates the project directory and installs the
prerequisite folders and files.
IDE
Pycharm was chosen for this setup guide because of its ease of use
and additional features. While pycharm is the goto IDE for this and
future projects, nano, VIM, Sublime Text, etc may be used instead. [[If
asked, always add new files to git]]
Pycharm
:PYCHARM
Open an existing project and choose the top-level project folder.
Settings.py
DATABASES = {
defaults:{
ENGINE: django.backends.sqlite3,
NAME: os.path.join[BASE_DIR, db.sqlite3],
Database engine can be chosen or switched at any time.
Postgre, SQLAlchemy, mongoDB, sqlite3,
Migrate
$./manage.py migrate
Creates initial database entries.
run_server
$./manage.py runserver
Runs the default dev server. http://127.0.0.1:8000/
Install GIT
$sudo apt-get install git

Currently, Im running mac. This guide will eventually be setup


for linux environment. Mac version has optional GUI, which Ive
also started using.
Git configuration
$git config global user.name Douglas J. Hajj
$git config global user.email precognist@gmail.com
Sets the global user configuration, Username and email.
Create repository
$git init
Once moved into the project repository, run command.
Collect and add python/pycharm gitignore
$source /usr/bin/virtualenvwrapper.sh
Collect and add python
https://github.com/github/gitignore/blob/master/Python.gitignore
and pycharm
https://github.com/github/gitignore/blob/master/Global/JetBrains.
gitignore. Gitignore contents. Add to gitignore file found in
project directory - .gitignore
Add configuration and gitignore chnges to git
$git add .
Adds changes to the git repo, readies for commit.
Check git status
$git status
Shows the current state of the git repo.
Commit to git
$git commit m Initial commit(Commit comments).
Commits the added changes to the master branch of the current
repo.

Chapter Two Templates, Views, & URLs


This chapter outlines the requirements of building a simple app
and the workings of templates, views, and urls.
App
$./manage.py startapp appname
From project directory, this command creates and new app
template.
Add to git
$git add appname
Add newly created app to commit list.
project settings.py
INSTALLED_APPS = (
appname,
)
Add appname to the bottom of INSTALLED_APPS.
Views.py
from django.shortcuts import render
def index(request):
return render(request, template.html)
Open appname views.py. Add this code to the file.
Templates Dir
Add templates directory, create template.html
Create a new directory where the template files will be stored.
templates.html
<body>
Hello World!
</body>
Add some text to the htmls body. Run server to test html body
text and application view. ./manage.py runserver
Urls.py
urlpatterns = [
url(r^$,appname.views.index, name=index),
]
add to urlpatterns in project directory. ./manage.py runserver
appname views.py
def appname(request):
return render(request, appname.html)

Add to the bottom of application views.py.


appname.html
<body>
Welcome to the appname app!
</body>
Add appname.html to templates directory, add text to Body.
appname urls
url(r^appname/, appname.views.appname, name=appname),
Add appname.html to templates directory. ./manage.py
runserver
Refactor
from django.conf.urls import url
from . import views
urlpatterns = [
url(r^$, views.appname, name=index),
]
To make the applications modular, adding urlpatterns to each
application allows them to be placed into alternate projects. Add
this code to application directorys urls.py
Project URLs
In project directory urls.py change:
url(r^appname/, appname.views.appname, name=appname),
To
url(r^appname/, include(appname.urls), name=appname),
Check server application and project URLs
$./manage.py runserver
Commit to git
$git commit am Add store app, view, templates and URL
commit recent changes to git. Comment.

Chapter Three Models, Databases, Migrations


and Admin
This chapter outlines the last part of getting a running django
project. Starting with models and databases, and finishing with
adding options to the django admin.
appname Models.py
from django.db import models
from django.utils import timezone
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
description = models.TextField()
publish_date = models.DateField(default=timezone.now)
price = models.DecimalField(decimal_places=2,
max_digits=8
stock = models.IntegerField(default=0)
Check the python version. At this time, the python version is
2.7.5.
Migrations
Django object relational mapping (ORM) is used to migrate and
change data in the database file. The default database file is
sqlite3.
Initial Migration
$workon projectname
$cd projectname/
$./manage.py makemigrations appname
Activates the named virtual environment. Enters the project
directory. Adds a migrations folder and 0001_initial.py.
ShowTables
$./manage.py sqlmigrate appname 0001
Shows what tables are going to be added to the database.
Migrate
$./manage.py migrate
Runs migration on all apps. $./manage.py migrate appname
will migrate the chosen app only.
Create SuperUser
$./manage.py createsuperuser
Creates a superuser account that is used to loginto the django
backend and manage the database directly.

appname admin.py
from django.contrib import admin
from .modules import modelClass
class BookAdmin(admin.ModelAdmin):
list_display = (title,author,price,stock)
admin.site.register(Book, BookAdmin)
Class grabs varsiables from appname/model.py to display in
django admin backend.
appname views.py modification
from django.shortcuts import render
from .models import Book
def index(request):
return render(request, template.html)
def store(request):
count = Book.objects.all().count()
context = {
count:count,
}
return render(request, store.html, context)
Import the created models and create control definitions. Add
model variables to context and add context to app render
request return.
appname.html modification
<body>
There are {{count}} books in the bookstore.
</body>
Add the created model context variable to the html.
Add migrations to git
$git add migrations/
$git commit am Git commit comment
Add the newly created migrations folder to git and add to repo.

Chapter Four Creating User Authentication


This chapter outlines the creation of user signup, authentication,
and social media account connections.
Checking out third party apps
$git checkout b auth-user
Check out third party app auth-user from git for user
authentication. Startup project with $workon projectname
Install django-registration
$pip install django-registration-redux==1.1
Check the python version. At this time, the python version is
2.7.5.
Django requirements
$pip freeze > requirements.txt
Pipeout the django project plugins/requirements for installations,
Make sure in the top-level project directory. Add to git: git add
requirements.txt
Settings.py
# Registration
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
Add to the base of the settings.py file found in the top layer of
the project directory.
Urls.py
url(r^accounts/,include(registration.backends.default.url
s)),
Check the python version. At this time, the python version is
2.7.5.
Registration Templates
Add registration folder to templates directory, create
activation_complete.html
activation_complete.html modification
<body>
<h1>Your account has been activated!</h1>
</body>
Activation acknowledgement.
activate.html modification
<body>
There was a problem activating your account.

</body>
Activation problem.
registration_form.html modification
<body>
<form method=post action=.>
{% csrf_token %}
{{form.as_p}}
<input type=submit value=Register />
</form>
</body>
Activation form.
registration_complete.html modification
<body>
Thanks for registering! Expect an email.
</body>
Registration completion.
Activation_email.txt modification
Greetings,
You just signed up at {{ site.name }}. Please click the
link to activate it:
http://{{site.domain }}{$ url registration_activate
activation_key %}
This link is valid for {{ registration_days }}. If it wasnt
you that signed up, dont panick. Its safe to ignore this
email.
Outgoing activation email.
Activation_email_subject.txt modification
<body>
Website Name
</body>
Activation problem.
Show migrations to this point
$./manage.py showmigrations
Shows current migration states. $./manage.py migrate to
complete the steps. Git commit.

Chapter - Title
This chapter outlines the setup of a distributed version control
system (DVCS). Install GIT.
Python
$python V
Check the python version. At this time, the python version is
2.7.5.
SubPython
$python V
Check the python version. At this time, the python version is
2.7.5.

Anda mungkin juga menyukai