How to write view.py for (status) check in models.py
How do I select the select option posted to models
from views.py? In models.py, I have given choices
parameters [draft,published]
.
I am using forms to collect input and store it in a database. All content works well except this one.
NameError at / blog / post / new /
name 'Published' is undefined
models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager,self).get_queryset().filter(status='published')
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='publish')
author=models.ForeignKey(User,related_name='blog_posts')
body=models.TextField()
publish=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')
objects=models.Manager()
published=PublishedManager()
views.py
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
import datetime
from .models import Post
from .forms import PostForm
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 )
return render(request,
'blog/post/detail.html',
{'post':post})
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.published = timezone.now()
post.slug=post.title
post.status=Published
post.save()
return redirect('post_detail', pk=post.pk)
else:
form = PostForm()
return render(request, 'blog/post/post_edit.html', {'form': form})
+3
source to share