Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django

 Django

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

Uğur Özyılmazel

February 27, 2016
Tweet

More Decks by Uğur Özyılmazel

Other Decks in Programming

Transcript

  1. 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
  2. # -*- 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
  3. 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()), ], ), ]
  4. 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()
  5. 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
  6. 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')
  7. 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))
  8. class PostUpdate(UpdateView): model = Post template_name = 'post-add-update.html' fields =

    ['title', 'body'] success_url = reverse_lazy('post-list')
  9. class PostList(ListView): model = Post template_name = 'post-list.html' ordering =

    ['-published_at'] queryset = Post.objects.filter(status=1)
  10. {% 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>
  11. {% 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>
  12. {% 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 %}
  13. {% 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 %}
  14. {% 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 %}
  15. <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>