Django Code Snippets

Useful & Common Code Snips in Python.
Based around Django.

INSTALL DJANGO

pip3 install django
or
pip3 install django==1.9

START PROJECT

django-admin startproject Project_Name

Created Files:

__init__.py = Tells python this is to be treated like a package.
settings.py = where SECRET_KEY is; add apps to INSTALLED_APPS.
urls.py = Well point to your apps; urlpatterns for url views.

Django Webapp structure.

https://youtu.be/FNQxxpM1yOs

Start Application:

python manage.py runserver

Create New App:

python manage.py startapp APP_NAME

FILE STRUCTURE

webapp folder

__init__.py = Treated as it’s own App.
admin.py = For administration.
apps.py = Holds the name of the App.
models.py = Database structures and metadata.
tests.py = Allows you to write tests to ensure updates don’t break anything.
urls.py = Controls what is served based on URL patterns (using regex).
views.py = Handles what the end-user sees and interacts with.

Django is a Model View Controller (MVC).
models.py = Model
views.py = View
urls.py = Controller
https://youtu.be/iZ5my3krEVM

You now have a Django MVC and WebApp set up, but they must be linked.

To link the webapp to the mrdj, add the “APP_NAME” to the mrdj/settings.py file.

mrdj/settings.py

Update urls.py file to include the Controller of the WebApp.

Add include after url, and url(r'^webapp/', include('webapp.urls)) to the urlpatterns array.

mrdj/urls.py

Create a new index view for the WebApp

In the webapp/views.py file:

from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>HEY!</h1>")
webapp/views.py

This will respond with HEY! when visiting mrdj/webapp url.

Add this new view to WebApp/urls.py

Allow this new index view to be rendered when visiting the root url for this WebApp.

from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
webapp/urls.py

This will import “views” which will allow python to render the index view for the webapp.
In the webapp/views.py file we defined a function “index” which contains the HTML for the “index” view, and in the urls.py file we are rendering that view when the url = “mjdj/webapp/” (webapp root).


Create Another New App — “personal”

New WebApp “personal”.

python manage.py startapp personal

Install App

In the mrdj/settings.py file, add ‘personal’ to INSTALLED_APPS.

INSTALLED_APPS = [
‘webapp’,
‘personal’,

Add personal.urls Path to mrdj/urls.py.

Create new urls.py for the Personal Webapp.

from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r’^webapp/’, include(‘webapp.urls’)),
url(r’^$’, views.index, name=’index’),
]

Update views.py in Personal

from django.shortcuts import render
def index(request):
return render(request, ‘personal/home.html’)

Create two new folders: templates & templates/personal


Cloud9 Workspace Django on Python3

Go into the Preferences (click the gear icon in the top-right corner), then click Language Support. Change the dropdown for Python Version to Python 3. Then, enter the following commands in the terminal:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.4 /usr/bin/python

a

Like what you read? Give Rio Weber a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.