Django além do admin

5,687 views
5,577 views

Published on

Apresentação "Django além do admin", realizado no ciclo de apresentações internas da Giran (Giran Siege).

Mais detalhes sobre o Giran Siege: http://blog.giran.com.br/2010/09/giran-siege-palestras-internas-da-giran/

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,687
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django além do admin

  1. 1. O Django é o “framework web paraperfeccionistas com prazos”... Esse cara ta me zoando... ... e o seu principal recurso é o Django Admin.
  2. 2. Além do que seus olhos podem ver!http://2.bp.blogspot.com/_WHZ6KCOzbjE/S-qISN_FKuI/AAAAAAAABq4/Np0J_Vsf_vk/s1600/RafikisWords.jpg
  3. 3. Django além do admin Francisco Souza francisco@franciscosouza.net www.franciscosouza.com
  4. 4. http://www.flickr.com/photos/41460120@N04/4217783923/ Quer dizer que o admin não é o principal recurso do Django?! Não é bem assim, mano...
  5. 5. http://www.flickr.com/photos/villes/1348742988/
  6. 6. então qual éhttp://www.flickr.com/photos/desiitaly/2201907500/
  7. 7. O principal recurso doDjango é o mais fundamental para a sua aplicação
  8. 8. Conheça os recursos!
  9. 9. class Product(models.Model): name = models.CharField(max_length=200) description = models.TextField() price = models.FloatField() creation_date = models.DateField(auto_now_add=True) last_modification_date = models.DateField(auto_now=True) hidden = models.BooleanField(default=False) image = models.ImageField(upload_to=images/products, max_length=200)
  10. 10. Salvando objetosdef new_product(request): """Renders a product form on GET requests and saves a product on POST requests""" form = ProductForm() if request.method == POST: form = ProductForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponse(Product saved.) return render_to_response(product_form.html, { form : form }, context_instance=RequestContext(request) )
  11. 11. urls.pyurlpatterns = patterns(, (r^new_product, views.new_product),)
  12. 12. E se for outro model?def new_product(request): """Renders a product form on GET requests and saves a product on POST requests""" form = ProductForm() if request.method == POST: form = ProductForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponse(Product saved.) return render_to_response(product_form.html, { form : form }, context_instance=RequestContext(request) )
  13. 13. E se for outro model?def new_category(request): """Renders a category form on GET requests and saves a category on POST requests""" form = CategoryForm() if request.method == POST: form = CategoryForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponse(Category saved.) return render_to_response(category_form.html, { form : form }, context_instance=RequestContext(request) )
  14. 14. Que tal outro?def new_customer(request): """Renders a customer form on GET requests and saves a customer on POST requests""" form = CustomerForm() if request.method == POST: form = CustomerForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponse(Customer saved.) return render_to_response(customer_form.html, { form : form }, context_instance=RequestContext(request) )
  15. 15. http://abhinavpmp.com/wp-content/uploads/repetition.jpgDont repeat yourself!
  16. 16. http://abhinavpmp.com/wp-content/uploads/repetition.jpg I dont yourself?!Dont repeat yourself!
  17. 17. Generic viewsurlpatterns = patterns(, (r^new_product,django.views.generic.create_update.create_object, { model: Product, post_save_redirect : /products/,template_name : product_form.html }),)
  18. 18. http://bit.ly/generic-views
  19. 19. AutenticaçãoLOGIN_URL = /login/LOGIN_REDIRECT_URL = /
  20. 20. Autenticaçãodef say_hello(request): """Says Hello world on GET requests""" return HttpResponse(Hello world!)
  21. 21. Autenticação@login_requireddef say_hello(request): """Says Hello world on GET requests""" return HttpResponse(Hello world!)
  22. 22. Django Formsclass ProductForm(forms.Form): name = forms.CharField(max_length=200) description = forms.TextField() price = forms.FloatField() hidden = forms.BooleanField(default=False) image = forms.ImageField(upload_to=images/products, max_length=200)
  23. 23. Django Model Formsclass ProductForm(forms.ModelForm): class Meta: model = Product
  24. 24. Validação sem dorclass ContactForm(forms.Form): name = forms.CharField(max_length=150) email = forms.EmailField(max_length=75) message = forms.TextField()
  25. 25. http://bit.ly/django-felds
  26. 26. Localizaçãoclass ContactForm(forms.Form): name = forms.CharField(max_length=150) email = forms.EmailField(max_length=75) cpf = BRCPFField() message = forms.CharField(widget=forms.Textarea)
  27. 27. Localizaçãoclass ContactForm(forms.Form): name = forms.CharField(max_length=150) email = forms.EmailField(max_length=75) cpf = BRCPFField() message = forms.CharField(widget=forms.Textarea) Valida CPF?! :O
  28. 28. http://bit.ly/django-l10n
  29. 29. Enviando e-mailclass ContactForm(forms.Form): name = forms.CharField(max_length=150) email = forms.EmailField(max_length=75) cpf = BRCPFField() message = forms.CharField(widget=forms.Textarea) def send_email(self): msg = "Hi, Im %(name)s. My message for you: %(message)s. My CPF is %(cpf)s." % self.cleaned_data send_mail(Contact, msg, self.cleaned_data[email],[dest@gmail.com], fail_silently=True)
  30. 30. django.core.mail.send_mailEMAIL_HOST = smtp.voce.comEMAIL_HOST_USER = nome@sobrenome.comEMAIL_HOST_PASSWORD = senha123EMAIL_PORT = 25EMAIL_USE_TLS = False
  31. 31. Enviando e-maildef contact(request): """Renders a contact form on GET requests and sends an email on POST requests""" form = ContactForm() if request.method == POST: form = ContactForm(request.POST) if form.is_valid(): form.send_email() return HttpResponse("Mail sent.") return render_to_response(contact_form.html, { form : form }, context_instance=RequestContext(request) )
  32. 32. django.test.client.Client from django.test.client import Client client = Client() response = client.get(/contact/) assert Name in response.contentTest client
  33. 33. http://bit.ly/django-testing
  34. 34. Chega!http://www.flickr.com/photos/jkonig/2056057464/
  35. 35. Antes de terminarmos...
  36. 36. Signals pre_save post_save pre_delete post_delete request_started request_fnished
  37. 37. http://bit.ly/django-signals
  38. 38. Tem mais?!
  39. 39. djangoproject.com
  40. 40. Obrigado! www.giran.com.brfrancisco@franciscosouza.net

×