Guest User

Untitled

a guest
Jan 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. from django.db import models
  2. from django.utils import timezone
  3. from django.contrib.auth.models import User
  4. from django.urls import reverse
  5.  
  6.  
  7. class Post(models.Model):
  8. STATUS_CHOICES = (
  9. ('draft','Draft'),
  10. ('published','Published'),
  11. )
  12. title = models.CharField(max_length=250)
  13. slug = models.SlugField(max_length=250,
  14. unique_for_date='published')
  15. author = models.ForeignKey(User,
  16. related_name='blog_posts',
  17. on_delete=models.CASCADE)
  18. body = models.TextField()
  19. published = models.DateTimeField(default=timezone.now)
  20. created = models.DateTimeField(auto_now_add=True)
  21. updated = models.DateTimeField(auto_now=True)
  22. status = models.CharField(max_length=10,
  23. choices = STATUS_CHOICES,
  24. default='draft')
  25. def get_absolute_url(self):
  26. return reverse('blog:post_detail',
  27. args=[self.publish.year,
  28. self.publish.strftime('%m'),
  29. self.publish.strftime('%d'),
  30. self.slug])
  31.  
  32. class Meta:
  33. ordering = ('-published',)
  34.  
  35.  
  36. def __str__(self):
  37. return self.title
  38.  
  39. from django.shortcuts import render, get_object_or_404
  40. from .models import Post
  41.  
  42. def post_list(request):
  43. posts = Post.published.all()
  44. return render(request,
  45. 'blog/post/list.html',
  46. {'posts': posts})
  47.  
  48. def post_detail(request, year, month, day, post):
  49. post = get_object_or_404(Post, slug=post,
  50. status='published',
  51. publish_year=year,
  52. publish_month=month,
  53. publish_day=day)
  54.  
  55. from django.shortcuts import render, get_object_or_404
  56. from .models import Post
  57. def post_list(request):
  58. posts = Post.published.all() ...
Add Comment
Please, Sign In to add comment