在我的django项目中,用户可以创建帖子.但是管理员必须先批准帖子.然后用户才能在他们的时间线中看到他们创建的帖子.用户可以在帖子细节页面中喜欢或不喜欢帖子.但是,当管理员从django管理面板登录来批准帖子并点击保存按钮时,它显示"喜欢"字段是必需的。我如何解决这个问题,以便管理员可以批准该帖子?
blog/models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth import get_user_model
from django.urls import reverse
from ckeditor.fields import RichTextField # first I installed ckeditor by this command: pip install django-ckeditor
# Create your models here.
class Category(models.Model):
cid = models.AutoField(primary_key=True)
category_name = models.CharField(max_length=100)
def __str__(self):
return self.category_name
class Post(models.Model):
aid = models.AutoField(primary_key=True)
image = models.ImageField(default='blog-default.png', upload_to='images/')
title = models.CharField(max_length=200)
# content = models.TextField()
content = RichTextField()
created = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
cid = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='specialization')
approved = models.BooleanField('Approved', default=False)
like = models.ManyToManyField(get_user_model(), related_name='likes')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk':self.pk})
@property
def total_likes(self):
return self.like.count()
users/models.py
from django.db import models
from blog.models import Category
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
cid = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
profile_pic = models.ImageField(default='default_person.jpg', upload_to='profile_pics')
blog/forms.py
from django import forms
from .models import Post, Comment
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'content', 'image', 'cid']
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control'}),
'content': forms.Textarea(attrs={'class': 'form-control'}),
'image': forms.FileInput(attrs={'class': 'form-control'}),
'cid': forms.Select(attrs={'class': 'form-control'}),
}
class EditForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'content', 'image', 'cid']
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control'}),
'content': forms.Textarea(attrs={'class': 'form-control'}),
'cid': forms.Select(attrs={'class': 'form-control'}),
}
users/forms.py
from django import forms
from django.contrib.auth import get_user_model # I changed the default User model. So I need to change the way I access it The right way to use it is get_user_model()
from django.contrib.auth.forms import UserCreationForm # it is used to create a new user
from blog.models import Category
User = get_user_model()
class UserRegisterForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['specialization'].required = True
email = forms.EmailField()
categories = Category.objects.all()
specialization = forms.ModelChoiceField(queryset=categories)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'specialization']
blog/post-detail.html
{% extends 'users/base.html' %}
{% block content %}
<div class="card mb-3">
<img class="card-img-top" src="{{ object.image.url }}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{ object.title }}</h5>
<p class="card-text">{{ object.content|safe }}</p>
<p class="card-text"> <b> Specialization: </b> {{ object.cid }} </p>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.aid %}">Edit</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.aid %}">Delete</a>
</div>
{% endif %}
<hr>
<form action="{% url 'post-like' object.pk %}" method="POST">
{% csrf_token %}
{% if post_is_liked %}
<button type="submit" name="post_id" value="{{object.aid}}" class="btn btn-danger btn-sm">Unlike</button>
{% else %}
<button type="submit" name="post_id" value="{{object.aid}}" class="btn btn-info btn-sm">Like</button>
{% endif %}
- {{ post.like.count }} Likes
</form>
</div>
<div class="card-footer text-secondary">
<a class="mr-2" href="{% url 'other-people-profile' object.author.username %}">{{ object.author }}</a>||
{{ object.created|date:"F d, Y" }}
</div>
</div>
{% endblock content %}
1条答案
按热度按时间jdzmm42g1#
类似字段未标记为可选。