我的PostUpdateView不发送任何数据到表单更新模板

问题描述 投票:0回答:1

error 我有完全相同的结构来更新论坛的帖子,所以它们确实有效,但文章的却不起作用。我的article_update_form模板简单没有从视图接收任何数据,因此slug是空的。

我的更新视图

class ArticlePostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = ArticlePost
    template_name = 'articles/article_form_update.html'
    form_class = ArticlePostForm

    def form_valid(self, form):
        form.instance.author = self.request.user
        messages.success(self.request, 'The article has been updated')
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user.id == post.article_users.user.id:
            return True
        return False

网址:

urlpatterns = [
    path("posts/<slug>/", articles, name="articles"),
    path('<int:page_id>', pages.views.articles, name='page'),
    path("detail/<slug>/", detail, name="article_details"),
    path("new_article", new_article, name="new_article"),
    path("new_article/create_post", login_required(ArticlePostCreateView.as_view()), name="create_article"),
    path("post/<slug:slug>/update/", ArticlePostUpdateView.as_view(), name="update_post_article"),
    path('categories_list', categories_list, name="categories_list"),
    path("search_article_post", search_article_post, name="search_article_post"),
    path("category/<slug>/", category_articles, name="category_articles"),
    path("sort_by/<str:typesort>", articles_sort, name="articles_sort"),
]

表格:

class ArticlePostForm(forms.ModelForm):
    class Meta:
        model = ArticlePost
        fields = ("title", "categories", "content", "article_tags", 'article_image')
        exclude = ["article_users"]

        widget = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'content': forms.TextInput(attrs={'class': 'form-control'}),
            'categories': forms.Select(attrs={'class': 'form-control'}),
            'article_tags': forms.Select(attrs={'class': 'form-control'}),
            'article_image': forms.Select(attrs={'class': 'form-control'}),
        }
    
    def __setitem__(self, i, elem):
        self.list[i] = elem

表格模板:

<div class="form-group">
                            <form method="POST" action="{% url 'update_post_article' slug=post.slug %}">
                                {% csrf_token %}
                                <div class="centered_create_post">
                                    {% render_field form.title placeholder="Enter title" class+="form-control each_post_create" %}
                                    {% render_field form.content  placeholder="Enter content" class+="form-control each_post_create pst_create_categories " %}
                                    {% render_field form.categories  class+="form-control each_post_create pst_create_categories " %}
                                    {% render_field form.article_image  class+="form-control each_post_create" %}
                                        <div class="pull-left"><button type="submit" class="btn_create_post">Update Post</button></div>
                                </div>
                                        <div class="clearfix"></div>
                                    </div>
                                    <div class="clearfix"></div>
                                </div>
                            </form>
                        </div>

当我尝试使用 get_context_data 时,我可以手动将帖子发送到表单模板,但表单不会呈现 forms.py 中的任何小部件。

python django django-forms django-templates django-generic-views
1个回答
0
投票

你应该改变:

<form method="POST" action="{% url 'update_post_article' slug=post.slug %}">

致:

<form method="POST" action="{% url 'update_post_article' post.slug %}">
© www.soinside.com 2019 - 2024. All rights reserved.