Django

 Django

3.Programlama Dilleri, 2016
Karabük Üniversitesi'nde yaptığım Sunum

033485f7e18823d4ccabb24e5cb4935f?s=128

Uğur Özyılmazel

February 27, 2016
Tweet

Transcript

  1. None
  2. Az kod yazarak hızlı web uygulaması geliştirmeyi kolaylaştıran bir Python

    kütüphanesi
  3. Uğur “vigo” Özyılmazel vigo vigobronx

  4. CRUD

  5. Create Read Update Delete

  6. INSERT INTO users (email, password) VALUES ('hello@me.com', '12345') CREATE

  7. SELECT * FROM users READ

  8. UPDATE users SET email = 'new@email.com' WHERE id = 1

    UPDATE
  9. DELETE FROM users WHERE id = 1 DELETE

  10. None
  11. Batteries included Pilleri de yanında

  12. DATABASE ABSTRACT

  13. Model View Template

  14. Model Template View

  15. Form

  16. Admin Panel

  17. Security

  18. I18N / L10N / TZ

  19. Authentication / Session

  20. Caching

  21. Email / File Upload

  22. BLOG id, status, published_at, title, body

  23. pip install django django-admin.py startproject pg16_django cd pg16_django/ python manage.py

    migrate python manage.py createsuperuser python manage.py startapp blog
  24. blog/ ├── migrations ├── __init__.py ├── admin.py ├── apps.py ├──

    models.py ├── tests.py └── views.py
  25. # -*- coding: utf-8 -*- from __future__ import unicode_literals from

    django.db import models POST_STATUS = ( (0, u"Kapalı"), (1, u"Yayında"), ) class Post(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) status = models.IntegerField(choices=POST_STATUS, default=1,) published_date = models.DateTimeField() title = models.CharField(max_length=255) body = models.TextField() def __unicode__(self): return u"%s" % self.title
  26. None
  27. python manage.py makemigrations python manage.py migrate

  28. operations = [ migrations.CreateModel( name='Post', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False,

    verbose_name='ID')), ('created_at', models.DateTimeField( auto_now_add=True)), ('updated_at', models.DateTimeField( auto_now=True)), ('status', models.IntegerField(choices=[ (0, ‘Kapal\u0131'), (1, 'Yay\u0131nda')], default=1)), ('published_date', models.DateTimeField()), ('title', models.CharField(max_length=255)), ('body', models.TextField()), ], ), ]
  29. Admin Panel

  30. None
  31. python manage.py runserver http://127.0.0.1:8000/admin/

  32. None
  33. None
  34. class Post(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) status =

    models.IntegerField(choices=POST_STATUS, default=1,) published_at = models.DateTimeField() title = models.CharField(max_length=255) body = models.TextField()
  35. python manage.py makemigrations

  36. Did you rename post.published_date to post.published_at (a DateTimeField)? [y/N] y

    Migrations for 'blog': 0002_auto_20160224_1150.py: - Rename field published_date on post to published_at
  37. None
  38. VIEW ve TEMPLATE

  39. CLASS BASED VIEW

  40. CRUD create

  41. class PostCreate(CreateView): model = Post template_name = 'post-add-update.html' fields =

    ['title', 'body', 'published_at'] initial = { 'published_at': datetime.datetime.now(), } success_url = reverse_lazy('post-list')
  42. CRUD read

  43. class PostDetail(DetailView): model = Post template_name = 'post-detail.html' def get_queryset(self):

    qs = super(PostDetail, self).get_queryset() return qs.filter(status=1, pk=self.kwargs.get('pk', None))
  44. CRUD update

  45. class PostUpdate(UpdateView): model = Post template_name = 'post-add-update.html' fields =

    ['title', 'body'] success_url = reverse_lazy('post-list')
  46. CRUD delete

  47. class PostDelete(DeleteView): model = Post template_name = 'post-check-delete.html' success_url =

    reverse_lazy('post-list')
  48. TÜM KAYITLAR

  49. class PostList(ListView): model = Post template_name = 'post-list.html' ordering =

    ['-published_at'] queryset = Post.objects.filter(status=1)
  50. TEMPLATE

  51. {% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta

    charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Kişisel Blog{% block html_title %}{% endblock %}</title> <link rel="stylesheet" href="{% static 'css/application.css' %}"> </head> <body> {% block content %}{% endblock %} </body> </html>
  52. {% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta

    charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Kişisel Blog{% block html_title %}{% endblock %}</title> <link rel="stylesheet" href="{% static 'css/application.css' %}"> </head> <body> {% block content %}{% endblock %} </body> </html>
  53. {% extends 'layout.html' %} {% load humanize %} {% block

    html_title %} : Postlar{% endblock %} {% block content %} {% include "header.html" %} <div class="container"> <div class="row"> <div class="col-xs-12"> <ul class="list-unstyled post-list"> {% for post in object_list %} <li> <a title="Detaylar için tıkla" href="{{ post. <time pubdate datetime="{{ post.published_at| <p class="lead">{{ post.body|striptags|trunca </li> {% endfor %} </ul> <a class="btn btn-lg btn-primary" href="{% url 'post- </div> </div> </div> {% include "footer.html" %} {% endblock content %}
  54. {% extends 'layout.html' %} {% load humanize %} {% block

    html_title %} : Postlar{% endblock %} {% block content %} {% include "header.html" %} <div class="container"> <div class="row"> <div class="col-xs-12"> <ul class="list-unstyled post-list"> {% for post in object_list %} <li> <a title="Detaylar için tıkla" href="{{ post. <time pubdate datetime="{{ post.published_at| <p class="lead">{{ post.body|striptags|trunca </li> {% endfor %} </ul> <a class="btn btn-lg btn-primary" href="{% url 'post- </div> </div> </div> {% include "footer.html" %} {% endblock content %}
  55. {% extends 'layout.html' %} {% load humanize %} {% block

    html_title %} : Postlar{% endblock %} {% block content %} {% include "header.html" %} <div class="container"> <div class="row"> <div class="col-xs-12"> <ul class="list-unstyled post-list"> {% for post in object_list %} <li> <a title="Detaylar için tıkla" href="{{ post. <time pubdate datetime="{{ post.published_at| <p class="lead">{{ post.body|striptags|trunca </li> {% endfor %} </ul> <a class="btn btn-lg btn-primary" href="{% url 'post- </div> </div> </div> {% include "footer.html" %} {% endblock content %}
  56. <ul class="list-unstyled post-list"> {% for post in object_list %} <li>

    <a title="Detaylar için tıkla" href="{{ post.get_absolute_url }}">{{ post.title }}</a> <time pubdate datetime="{{ post.published_at|date:"c" }}" title="{{ post.published_at }}”> {{ post.published_at|naturaltime }}</time> <p class=“lead"> {{ post.body|striptags|truncatewords:20 }} </p> </li> {% endfor %} </ul> <a class="btn btn-lg btn-primary” href="{% url 'post-create' %}">Yeni Ekle</a>
  57. {{ post.body|striptags|truncatewords:20 }}

  58. {{ post.published_at|naturaltime }}

  59. None
  60. Kaynaklar

  61. • http://tutorial.djangogirls.org/tr/ • https://docs.djangoproject.com/en/1.9/ • https://github.com/vigo/pg16_django • http://ccbv.co.uk/

  62. http://media.djangopony.com/img/magic-pony-django-wallpaper.png Django Pony Resmi

  63. Teşekkürler, sorular ?