如何显示帖子“适度”?在网站页面“帖子列表”中为管理员在Django 2.1上? [英] How display post "on moderation" for admin at site page "Post list" at Django 2.1?

查看:58
本文介绍了如何显示帖子“适度”?在网站页面“帖子列表”中为管理员在Django 2.1上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个询问


我想在站点页面帖子列表中为管理员(工作人员)显示在审核时存在的新帖子。 (模板 post_list.html),例如 scrin1 (帖子1-已发布;帖子2-适度)。普通用户可以看到发布列表仅针对已发布的帖子,请参见 scrin2。这是为了确保每个管理员都不会进入站点的管理面板,而是可以从站点页面发布更新中发布此帖子。 (模板 post_form.html),例如 scrin3。
我尝试使用views.py中的staff_member_required(在PostListView中为form_valid)执行此操作,但是没有成功。
如果帖子已被编辑,则在发布编辑版本之前也必须进行审核。


以下代码从管理面板显示帖子审核的实现。我只是不知道如何在网站页面上进行帖子(创建或更新)的审核(例如,不从网站页面 ... / post // update登录到管理面板),只需选中审核中的复选框即可)


models.py

  class Post(models.Model):
用户=模型.ForeignKey(用户,on_delete = models.CASCADE)
标题=模型.CharField(max_length = 50)
body =模型。 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):
标题=表格.CharField(必需=真)
正文=表格.CharField(widget = forms.Textarea)

类Meta :
模型=发布
字段= ['title','body']

views.py

  from .forms import PostForm 

类PostListView(generic.ListView):
模型=发布
paginate_by = 10

def get_queryset(self):
queryset = super(PostListView,self).get_queryset()
返回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()
返回响应


类PostUpdateView(PermissionRequiredMixin,UpdateView):
模型=发布
字段= ['title','body']
Permission_required ='blog.can_mark_retur ned'

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

  {%扩展了 base_generic.html %} 
{%块标题%}< title>帖子列表< / title> {%最终块%}
{%块内容%}
< h1>所有帖子< / h1> ;
{%if post_list%}
< ul>
{post_list中帖子的%}} {{post.user}}< / li>
{%endfor%}
< / ul>
{%else%}
< p>没有帖子。
{%endif%}
{%endblock%}

post_form.html

  {%扩展了 base_generic.html %} 
{%块内容%}
< form action = method = post enctype = multipart / form-data。
{%csrf_token%}
< table>
{{form.as_table}}
< / table>
< input type = submit值=提交 />
< / form>
{%endblock%}

Screen1 查看页面帖子列表作为管理员



Screen2 查看页面帖子列表,作为常规用户



Screen3 视图页面发布更新;作为管理员带有适度复选框


解决方案

如果我真的了解您的问题,则可以按如下所示更新视图。

  {%扩展了 base_generic.html%} 
{%块标题%}< title>帖子列表< / title> {%endblock%}
{%block content%}
< h1>所有帖子< / h1>
{%if post_list%}
< ul>
{post_list中帖子的%%}
< li>< a href = {{post.get_absolute_url}}> {{post.title}}< / a> {{post.user}}< / li>
{%如果是post.moderation和request.user.is_staff%}
< form action = {%url主持人批准 post.pk%} method = post>
{%csrf_token%}
< button type = submit>批准< / button>
{%endif%}
{%endfor%}
< / ul>
{%else%}
< 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):

如果request.method =='POST':
post = Post.objects.get(pk = kwargs ['post_id'])
post.moderation = True
post.save()
return HttpResponseRedirect('posts')


Previous Ask

I want to display a new post, which exist "on moderation", for the administrator (staff), at site page "Post list" (template "post_list.html"), for example "scrin1" (Post 1 - published; Post 2 - "on moderation"). Regular user can see "Post list" only with published posts, see "scrin2". This to ensure that each admin didn't enter in to the admin panel of the site, but can published this post from the site page "Post update" (template "post_form.html"), for example "scrin3". I tried to do it with staff_member_required in views.py (form_valid in PostListView), but it didn't work out. If post was edited, must go moderation too before publish edited version.

The following code shows the implementation of moderation of posts from the admin panel. I just don’t know how to make the moderation of posts (created or updated) from the site page (without logging in to the admin panel, for example, from the site page "... / post / / update", simply by ticking the checkbox in "Moderation")

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 %}

Screen1 view page "Posts list" as "Admin"

Screen2 view page "Posts list" as "Regular user"

Screen3 view page "Post update" as "Admin" with "Moderation" checkbox

解决方案

you can update your view as below, if i really understand your problem.

{% 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 %}

In your urls.py

urlpatterns = [ 
     path('moderator-approval/<int:post_id>/', views.moderator_approval_view, name="moderator-approval")
]

In yours 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')

这篇关于如何显示帖子“适度”?在网站页面“帖子列表”中为管理员在Django 2.1上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆