Django Girls Mbale [victor's sessions]

Preview:

DESCRIPTION

A bunch of concepts in Django

Citation preview

Django victor.miclo.kisi

MTV??

Model

Template

View

views.pydef some_view(request):

return render_to_response(“path/to/template.html”, {}, RequestContext(request))

HTML<html>

<head>

<title>Hello world</title>

</head>

<body>

<p>Hi</p>

</body>

</html>

Tags in HTML

● Paragraphs● Headers● Divs● Section● asection● etc.● See W3C tutorials and plenty of Online

tutorials

`style.css`

p{ color: red;}

CSS (Cascading Style Sheets)

CSS (Cascading Style Sheets)

`style.css`

.food_section{ color: red;}

CSS (Cascading Style Sheets)

`style.css`

#food_section{ color: red;}

`http://getbootstrap.com`

<head>

<link rel=”styleshet” href=”path/to/bootstrap.css”>

</head>

<body>

...some code…

<script src=”path/to/bootstrap.js”></script>

Bootstrap

`http://www.google.com/fonts`

<head>

<link rel=”styleshet” href=”path/to/bootstrap.css”>

<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>

</head>

<body>...</body>

Google Fonts

URLs..

What is URL?

URLs..

Maps your “view functions” to a URL.

● someone visits a URL● URL tells django what to do● Django returns a set of HTML (templates)

Templates [part 1]

Just HTML… take it EASY!

Templatesmysite

⇒ mysite

===> templates/mysite

===> [other folders]

⇒ app1

===> templates/app1

===> [other folders]

⇒ app2

===> templates/app2

===> [other folders]

forms [Non-model based]

● Create `forms.py` in your Django Application

● import the Form class

● define what you’ll collect

from django import forms

class MessageForm(forms.Form):

number = forms.CharField()

text = forms.CharField()

forms [Non-model based]

from form import MessageForm

def new_message(request):

form = MessageForm()

if request.method == “POST”:

form = MessageForm(request.POST)

if form.valid():

form.save() # save

else:

pass # handle errors!

return render_to_response(‘messages/new.html’, {‘form’:form}, RequestContext(request))

Hooking up forms in Views

Shortcuts!!!

forms [Model based]

● Create `forms.py` in your Django Application

● import the Form class

● define what you’ll collect

from django import forms

from .models import Message

class MessageForm(forms.ModelForm):

model = Message

forms [Model based]

from form import MessageForm

def new_message(request):

form = MessageForm()

if request.method == “POST”:

form = MessageForm(request.POST)

if form.valid():

form.save() # save

else:

pass # handle errors!

return render_to_response(‘messages/new.html’, {‘form’:form}, RequestContext(request))

Hooking up forms in Views (similar)

<form

Hooking up forms in HTML

Some advanced topics...

Template inheritanceBase HTML filee.g. base.html

Profile page

e.g. profile.html

Messages page

e.g. messages.html

Template inheritanceBase HTML filee.g. base.html

Profile page

e.g. profile.html

Messages page

e.g. messages.html

Base HTML has your site-wide designs and you don’t have to repeat the design on different pages.

Template inheritanceBase HTML filee.g. base.html

code sample...

<html><head><!-- CSS or js here --></head><body>{% block content %}{% endblock %}</body>

</html>

{% extends “base.html” %}{% block content %}

<div>Hi {{ name }}. You are seeing your profile.</div>

{% endblock %}

Template inheritanceProfile pagee.g. profiles.html

code sample...

What next…?

● Do the django girls tutorial● Do the 6 part django tutorial (http:

//djangoproject.com)● Google a lot! ● Do many projects..● Check out TDD

Recommended