Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Django Crash Course

No description
by

Nicu Badescu

on 25 November 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Django Crash Course

It uses MVC (Model - View - Controller) as a software architecture pattern.

However, it is commonly referred to as an MTV (Model - Template - View) web framework.
Models
Defining a model (in models.py file):

from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
votes = models.IntegerField(default=0)
URLConfs
In urls.py:

from django.conf.urls import patterns, url
from myapp.views import home, view_to_be_called1, view_to_be_called2

urlpatterns = patterns('',
url(r'^$', home),
url(r'^articles/2003/$', view_to_be_called_1),
url(r'^articles/(\d+/$', view_to_be_called_2),
)
Templates
- similar to Jiinja2

<ul>
{% for item in item_list %}
<li> {{ item }} </li>
{% endfor %}
</ul>
Django Crash Course
What is Django?
"The web framework for perfectionists with deadlines"

Similar to Flask, Ruby on Rails, Zope, etc.
Nicu Bădescu
nbadescu@rosedu.org

A little bit of history
- started in 2003 at the Lawrence Journal-World newspaper
Creating, retrieving, updating and saving a model

p = Poll()
p.question = "How are you?"
p.pub_date = datetime.now()
p.votes = 10
p.save()

another_p = Poll.objects.get(votes=10)
yet_another_p = Poll.objects.filter(question__icontains='how')

Forms
Defining a form (in forms.py file):

from django import forms

class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)

Creating a form:
form = ContactForm()

Creating a form from a Python dictionary:
form = ContactForm(data)

Testing is a form is valid:
form.is_valid()

Displaying a form in the template:
form.as_p()
form.as_table()
form.as_ul()

Views
from django.shortcuts import render
import datetime

def current_datetime(request):
now = datetime.datetime.now()
d = {'current_time': now}
return render(request, 'a_template.html', d)
How to perform a redirect

from django.shortcuts import redirect
def redirect_view(request):
return redirect('nume_URL')
Form processing

from django.shortcuts import render, redirect

def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form

return render(request, 'contact.html', {
'form': form,
})



Mini Project: A simple URL Shortener
Full transcript