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. 1.
  2. 4.
  3. 10.
  4. 15.
  5. 17.
  6. 20.
  7. 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
  8. 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
  9. 26.
  10. 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()), ], ), ]
  11. 30.
  12. 32.
  13. 33.
  14. 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()
  15. 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
  16. 37.
  17. 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')
  18. 42.
  19. 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))
  20. 45.

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

    ['title', 'body'] success_url = reverse_lazy('post-list')
  21. 49.

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

    ['-published_at'] queryset = Post.objects.filter(status=1)
  22. 50.
  23. 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>
  24. 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>
  25. 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 %}
  26. 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 %}
  27. 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 %}
  28. 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>
  29. 59.
  30. 60.