O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Tango with django

343 visualizações

Publicada em

The talk was presented at PyDelhi conf 2017 (https://conference.pydelhi.org/). Core motive is to teach Django framework to user who wanted to learn it or want to start development in Django.

Publicada em: Software
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Tango with django

  1. 1. Tango with Django By Jaysinh Shukla
  2. 2. Speaker Description Self employed Pythonista Github: http://github.com/ultimatecoder, http://github.com/jsh-odoo Twitter: @jaysinhp Emai: jaysinhp@gmail.com IRC: thebigj Blog: http://blog.jaysinh.com
  3. 3. What is Django? ● Django is Python’s most popular web framework. ● Lawrence Journal-World is the popular daily newspaper publishing company who started developing web framework for publishing news over website. That framework is known as the Django web framework. ● At present the development is going under the Django Software Foundation ● Initially it was started before 10 years!
  4. 4. Benefits of Django ● Open source ● Large community base ● MVC ● Secure ● Fast Web application development ● Scalable
  5. 5. Who is using Django? ● Pinterest ● Instagram ● Mozilla ● Bitbucket ● NASA ● Open Stack
  6. 6. MVC vs MVT ● Django is MVC but usually people call it Model View Template ● The Model part is used for all the Data Base operations and managing record as object ● View part is used for managing, filtering, creating, deleting records of Model ● Template part is used for combining model object with the HTML templates
  7. 7. Create virtual environment ● You can use `venv` builtin module of Python. ○ `python3 -m venv my_virtualenv` ● Virtualenvwrapper ○ https://virtualenvwrapper.readthedocs.io/en/latest/ ● Virtualenv ○ https://pypi.python.org/pypi/virtualenv
  8. 8. Make requirements.txt ● Always dump your installation package to `requirements.txt` file. ○ $>pip freeze > requirements.txt ● The requirements.txt is a not a fix file name. It is just a convention.
  9. 9. Create a website ● Create website first ○ django-admin startproject mysite ● Here, `mysite` is name of website. ● Mysite directory ● ├── manage.py ● ├── mysite
  10. 10. Project vs App ● App is pluggable project ● Django follows DRY principle ● Project is a website in which there can be more than one collection of app.
  11. 11. Start app ● Startapp ○ `python manage.py startapp blog` ● Mysite ● │ ├── admin.py ● │ ├── __init__.py ● │ ├── models.py
  12. 12. Blog Post model structure ● Title ● Text ● Create date ● Published ● Author
  13. 13. Adding fields to model class Post(models.Model): title = models.CharField(max_length=200) text = models.TextField() created_date = models.DateTimeField(auto_now_add=True) published = models.BooleanField(default=False) author = models.ForeignKey(User, blank=True, null=True) def __str__(self): return self.title
  14. 14. A walk on Django migrations ● Migrations are app specific thing ● Always commit them ● Why migrations? ● Django will warn when you will make incompatible migrations ● ├── migrations ● │ │ ├── 0001_initial.py
  15. 15. Introduction to Django contribution modules ● https://docs.djangoproject.com/en/1.8/ref/contrib/ ● django.contrib.auth ● django.contrib.contenttypes ● django.contrib.sessions ● django.contrib.messages ● django.contrib.staticfiles
  16. 16. Introduction to Django admin ● Django admin panel is for managing website ● Do not make it available to all the users ● You can customize it, but remember it is for admin
  17. 17. Createsuperuser ● `python manage.py createsuperuser` ● Super user has all the authority.
  18. 18. Django shell ● model.objects.all() ● model.objects.filter() ● model.objects.get() ○ model.DoesNotExists ● Chaining ● len()
  19. 19. Creating view ● A view where all the blog posts are displayed in one title ● A view where single blog post is viewed
  20. 20. Adding templates ● Create templates dir under application ○ Templates/blog/base.html ○ templates/blog/list.html ○ templates/blog/single.html
  21. 21. Base template ● <html> ● <head> ● <title>{% block title %} {% endblock %} </title> ● </head> ● <body> ● {% block body %} {% endblock %}
  22. 22. Single Post ● {% extends 'blog/base.html' %} ● {% block title %} {{ post.title }} {% endblock %} ● {% block body %} ● <table>
  23. 23. List view ● {% extends 'blog/base.html' %} ● {% block title %} List of blogposts {% endblock %} ● {% block body %} ● {% for post in posts %} ● <table><tr><td><a href="{% url 'single_post' post.id %}">{{ post.title }}</a> </td></tr>
  24. 24. Type of views ● Function based views ● Class based views ● Built in views
  25. 25. Understanding Regex ● ^ ● $ ● () ● ?P ● <variable_name> ● https://docs.python.org/3.4/library/re.html
  26. 26. Create urls Blog ● List post(http://localhost:8000/post/) ○ url(r'^$', views.list_post), ● Single post (http://localhost:8000/post/id) ○ url(r'^(?P<post_id>[0-9]+)/$', views.single_post, name='single_post')
  27. 27. Creating views ● def single_post(request, post_id): ● try: ● post = Post.objects.get(id=post_id) ● except Post.DoesNotExists: ● return Http404("Post didn't found") ● return render(request, 'blog/post.html', {'post': post})
  28. 28. How to learn Django? ● Read tutorials section divided into 6 parts of Django https://media.readthedocs.org/pdf/django/1.8.x/django.pdf ● Contribute to any live project ○ Make website for your community ○ Try to SaaSify (SaaS) any existing tool ○ Help Pythonexpress.in (https://github.com/pythonindia/wye) ○ Do volunteering
  29. 29. Feedback https://goo.gl/forms/YIqstv2 cmoyLjZul2
  30. 30. Q & A

×