简体   繁体   中英

Only approved users should post blogs in django

I have build a blog based web-application using Django, Where users can login and wrote articles and those articles are being posted after admin approval. But right now all users can post articles. I want only those users to post the articles which are approved. I am using default Django user model. From users I only create a profile model. My users models.py is:

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
# Create your models here.
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pic')
    posting_post  = models.BooleanField(default=False)

    def __str__(self):
        return f'{self.user.username} Profile'

    def save(self , *args, **kwargs):
        super().save()

My postcreate view is:

class PostCreateView(TagMixin, LoginRequiredMixin, CreateView):
    model = Post
    fields = ['title', 'content', 'image',]


    def form_valid(self, form):
        form.instance.author = self.request.user
        form.save()
        return HttpResponseRedirect('admin-approval')

and my html for create post is:

 {% if user.is_authenticated %}
        <li class="nav-item">
          <a class="nav-link" href="{% url 'post-create'%}">New Post</a>
        </li>
        <li class="nav-item">

Easy, add a BooleanField can_post and then when the user log in, handle it the same way you are handling profile_image , then check it in the it condition in template.

Add a boolean field in the blog post model named approved or not and set default False, and in your views user query to exlude objects where approved=False.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM