我想在管理员页面(工作人员)的网站页面“帖子列表”(模板“ post_list.html”)上显示一个新帖子,该帖子“适中”,例如“ scrin1”(帖子1-已发布;帖子2-“适度”)。普通用户只能在已发布的帖子中看到“帖子列表”,请参阅“ scrin2”。这是为了确保每个管理员都不会进入站点的管理面板,而是可以从站点页面“发布更新”(模板“ post_form.html”)(例如“ scrin3”)发布此帖子。 我试图用views.py(在PostListView中的form_valid)中的staff_member_required来做到这一点,但是没有成功。 如果帖子已被编辑,则在发布已编辑版本之前也必须进行审核。
以下代码显示了管理面板中帖子审核的实现。我只是不知道如何通过站点页面(而不是登录到管理面板,例如,从站点页面“ ... / post // update”)来对帖子(创建或更新)进行审核选中“审核”中的复选框)
models.py
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=50)
body = models.TextField()
moderation = models.BooleanField(default=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
forms.py
from .models import Post
class PostForm(forms.ModelForm):
title = forms.CharField(required=True)
body = forms.CharField(widget=forms.Textarea)
class Meta:
model = Post
fields = ['title', 'body']
views.py
from .forms import PostForm
class PostListView(generic.ListView):
model = Post
paginate_by = 10
def get_queryset(self):
queryset = super(PostListView, self).get_queryset()
return queryset.filter(moderation=True)
class PostCreateView(FormView):
form_class = PostForm
template_name = 'blog/post_form.html'
success_url = reverse_lazy('posts')
def form_valid(self, form):
response = super(PostCreateView, self).form_valid(form)
form.instance.user = self.request.user
form.save()
return response
class PostUpdateView(PermissionRequiredMixin, UpdateView):
model = Post
fields = ['title', 'body']
permission_required = 'blog.can_mark_returned'
admin.py
from .models import Post
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'user', 'moderation')
admin.site.register(Post, PostAdmin)
urls.py
urlpatterns = [
url(r'^posts/$', views.PostListView.as_view(), name='posts'),
url(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), name='post-detail'),
url(r'^post/create/$', views.PostCreateView.as_view(), name='post_create'),
url(r'^post/(?P<pk>\d+)/update/$', views.PostUpdateView.as_view(), name='post_update'),
]
post_list.html
{% extends "base_generic.html" %}
{% block title %}<title>Posts list</title>{% endblock %}
{% block content %}
<h1>All posts</h1>
{% if post_list %}
<ul>
{% for post in post_list %}
<li><a href="{{ post.get_absolute_url }}">{{ post.title }}</a> {{ post.user }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no posts.</p>
{% endif %}
{% endblock %}
post_form.html
{% extends "base_generic.html" %}
{% block content %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}
屏幕1 以“管理员”身份查看页面“帖子列表”
Screen2 以“常规用户”的身份查看页面“帖子列表”
Screen3 视图页面,将“发布更新”作为“管理员”并带有“审核”复选框
答案 0 :(得分:0)
如果我真的了解您的问题,则可以如下更新视图。
{% extends "base_generic.html" %}
{% block title %}<title>Posts list</title>{% endblock %}
{% block content %}
<h1>All posts</h1>
{% if post_list %}
<ul>
{% for post in post_list %}
<li><a href="{{ post.get_absolute_url }}">{{ post.title }}</a> {{ post.user }}</li>
{% if post.moderation and request.user.is_staff %}
<form action="{% url 'moderator-approval' post.pk %}" method="post">
{% csrf_token %}
<button type="submit">Approve</button>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>There are no posts.</p>
{% endif %}
{% endblock %}
在您的urls.py
urlpatterns = [
path('moderator-approval/<int:post_id>/', views.moderator_approval_view, name="moderator-approval")
]
在您的views.py
def moderator_approval_view(request, **kwargs):
if request.method == 'POST':
post = Post.objects.get(pk=kwargs['post_id'])
post.moderation = True
post.save()
return HttpResponseRedirect('posts')