Desenvolvimento web usando django

Preview:

DESCRIPTION

 

Citation preview

DESENVOLVIMENTO WEB USANDO

DJANGO

YURIMALHEIROS@

YURIMALHEIROS@GMAIL.COM

TECNOLOGIA

TECNOLOGIA?

TECNOLOGIA?

”“SIMPLE IS

BETTER THAN

COMPLEX

FRAMEWORKS

“SE VI MAISLONGE FOI POR ESTAR SOBREOS OMBROS

DE GIGANTES

UM POUCO DE

HISTÓRIA...

MODEL

VIEW

CONTROLLER

MODEL

TEMPLATE

CONTROLLER

MODEL

TEMPLATE

VIEW

CODIFICANDO

django-admin startproject mymusic

mymusic

manage.py

settings.py

urls.py

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'db.sqlite',

'USER': '‘, 'PASSWORD': '‘, 'HOST': '‘, 'PORT': '‘, }

}

settings.py

manage.py startapp collection

collection

models.py

tests.py

views.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

from django.db import models

class Album(models.Model): artist = models.CharField(max_length=200) name = models.CharField(max_length=200) year = models.IntegerField() cover = models.URLField(verify_exists=False, blank=True)

def __unicode__(self): return "%s by %s" % (self.name, self.artist)

collection/models.py

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'collection',)

settings.py

manage.py syncdb

add/

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('', url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('', url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('', url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('', url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('', url(r'^add/$', collection_views.add),

)

urls.py

from django.forms import ModelFormfrom collection.models import Album

class AlbumForm(ModelForm): class Meta: model = Album

exclude = (‘cover’,)

collection/forms.py

from django.forms import ModelFormfrom collection.models import Album

class AlbumForm(ModelForm): class Meta: model = Album

exclude = (‘cover’,)

collection/forms.py

from django.forms import ModelFormfrom collection.models import Album

class AlbumForm(ModelForm): class Meta: model = Album

exclude = (‘cover’,)

collection/forms.py

from django.forms import ModelFormfrom collection.models import Album

class AlbumForm(ModelForm): class Meta: model = Album

exclude = (‘cover’,)

collection/forms.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

def add(request): if request.method == 'POST': form = AlbumForm(data=request.POST)

if form.is_valid(): album = Album() album.name = form.cleaned_data['name'] album.artist = form.cleaned_data['artist'] album.year = form.cleaned_data['year'] album.cover = get_cover_url(album.artist, album.name) album.save()

collection/views.py

else: form = AlbumForm()

return render_to_response('add.html', {'form' : form}, context_instance=RequestContext(request))

collection/views.py

else: form = AlbumForm()

return render_to_response('add.html', {'form' : form}, context_instance=RequestContext(request))

collection/views.py

else: form = AlbumForm()

return render_to_response('add.html', {'form' : form}, context_instance=RequestContext(request))

collection/views.py

else: form = AlbumForm()

return render_to_response('add.html', {'form' : form}, context_instance=RequestContext(request))

collection/views.py

else: form = AlbumForm()

return render_to_response('add.html', {'form' : form}, context_instance=RequestContext(request))

collection/views.py

settings.py

import os

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

TEMPLATE_DIRS = (os.path.join(PROJECT_PATH, 'templates'),

)

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

<form action="." method="post" enctype="multipart/form-data"> {% csrf_token %} {% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %}

<p><button type="submit">Adicionar</button></p></form>

templates/add.html

manage.py runserver

/

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),

)

urls.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

def index(request): albums = Album.objects.all().order_by('name')

return render_to_response('index.html', {'albums' : albums}, context_instance=RequestContext(request))

collection/views.py

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /></p>{% endfor %}

templates/index.html

delete/

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

from django.conf.urls.defaults import *from collection import views as collection_views

urlpatterns = patterns('',url(r'^$', collection_views.index, name='album_index'),url(r'^add/$', collection_views.add),url(r'^delete/(?P<album_id>\d+)/$', collection_views.delete, name='album_delete'),

)

urls.py

def delete(request, album_id): album = Album.objects.get(pk=album_id) album.delete() return redirect('album_index')

collection/views.py

def delete(request, album_id): album = Album.objects.get(pk=album_id) album.delete() return redirect('album_index')

collection/views.py

def delete(request, album_id): album = Album.objects.get(pk=album_id) album.delete() return redirect('album_index')

collection/views.py

def delete(request, album_id): album = Album.objects.get(pk=album_id) album.delete() return redirect('album_index')

collection/views.py

{% for album in albums %}<p> {% if album.cover %} <img src="{{album.cover}}" /> {% endif %}

{{album.name}}<br /> {{album.artist}}<br /> <a href={% url album_delete album.pk %}>apagar</a></p>{% endfor %}

templates/index.html

GITHUB.COM/YURIMALHEIR

OS

BÁSICO

PYTHON

FERRAMENTA

COMUNIDADE

BOA ESCOLHA :)

DÚVIDAS?

Recommended