Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.db import models
- from django.utils import timezone
- from django.contrib.auth.models import User
- from django.urls import reverse
- class Post(models.Model):
- STATUS_CHOICES = (
- ('draft','Draft'),
- ('published','Published'),
- )
- title = models.CharField(max_length=250)
- slug = models.SlugField(max_length=250,
- unique_for_date='published')
- author = models.ForeignKey(User,
- related_name='blog_posts',
- on_delete=models.CASCADE)
- body = models.TextField()
- published = models.DateTimeField(default=timezone.now)
- created = models.DateTimeField(auto_now_add=True)
- updated = models.DateTimeField(auto_now=True)
- status = models.CharField(max_length=10,
- choices = STATUS_CHOICES,
- default='draft')
- def get_absolute_url(self):
- return reverse('blog:post_detail',
- args=[self.publish.year,
- self.publish.strftime('%m'),
- self.publish.strftime('%d'),
- self.slug])
- class Meta:
- ordering = ('-published',)
- def __str__(self):
- return self.title
- from django.shortcuts import render, get_object_or_404
- from .models import Post
- def post_list(request):
- posts = Post.published.all()
- return render(request,
- 'blog/post/list.html',
- {'posts': posts})
- def post_detail(request, year, month, day, post):
- post = get_object_or_404(Post, slug=post,
- status='published',
- publish_year=year,
- publish_month=month,
- publish_day=day)
- from django.shortcuts import render, get_object_or_404
- from .models import Post
- def post_list(request):
- posts = Post.published.all() ...
Add Comment
Please, Sign In to add comment