简体   繁体   English

Django中FormView的POST方法?

[英]POST method of FormView in Django?

Edit: just used a new listing for when user submits the user to search on though I am not sure how to get what user was submitted still in my QueryUser class (the id of 24 is just a test, oh and I tried to add pagination but ripped it out because it would break when someone clicked a new page, would go back to the Query.html posted below, the crispy forms not the data returned)编辑:只是在用户提交用户进行搜索时使用了一个新列表,尽管我不确定如何获取仍在我的 QueryUser 类中提交的用户(24 的 id 只是一个测试,哦,我尝试添加分页但将其撕掉,因为当有人单击新页面时它会中断,会返回到下面发布的 Query.html,酥脆的表单而不是返回的数据)

class QueryUser(LoginRequiredMixin, FormView):
    model = OnSiteLog
    form_class = QueryForm
    template_name = 'log/query.html'
    success_url = '/'
    def form_valid(self, form, *args, **kwargs):
        user = form.cleaned_data.get('user')

    def post(self, request, *args, **kwargs):
        object_list = OnSiteLog.objects.filter(user_id=24).order_by('-checkIn')
        super(QueryUser, self).post(request, *args, **kwargs)
        page = request.GET.get('page', 1)

        paginator = Paginator(object_list, 10) # 3 posts in each page
      #make a map into the object list
      
        try:
            log_pages = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer deliver the first page
            log_pages = paginator.page(1)
        except EmptyPage:
            # If page is out of range deliver last page of results
            log_pages = paginator.page(paginator.num_pages)

        return render(request, 'log/query2.html', {'page':page, 'log':object_list, 'object_list':log_pages})

I am tring to understand how works Class Based Views in Django.我想了解 Django 中基于类的视图是如何工作的。 I have next working code with View .我有View下一个工作代码。 I want to write that code with the help of FormView .我想在FormView的帮助下编写该代码。 Is my `FormView code correct?我的`FormView 代码正确吗? I need someone who can analyze that code and say where is my mistakes.我需要有人可以分析该代码并说出我的错误在哪里。 I would be grateful for any help.我将不胜感激任何帮助。

Right now get method works.现在get method有效。 The problem is when I try to submit form.问题是当我尝试submit表单时。 Form dont disappear.表格不会消失。 In the same time in console I see this url: "POST /url/ HTTP/1.1" 200 1891 .同时在控制台中我看到这个 url: "POST /url/ HTTP/1.1" 200 1891 In database I see that new article was created.在数据库中,我看到创建了新文章。 How to fix this strange problem with post method ?如何使用post method解决这个奇怪的问题?

CBV with View:带视图的 CBV:

class ArticleCreateView(FormView):
    template_name = 'article/create_article.html'
    form_class = ArticleForm

    def post(self, request):
        data = dict()
        article_create_form = ArticleForm(request.POST, request.FILES)
        if article_create_form.is_valid():
            article_create_form.save()
            data['form_is_valid'] = True
            context = {
                'articles': Article.objects.all()
            }
            data['html_articles'] = render_to_string(
                'article/articles.html',
                context
            )
        else:
            data['form_is_valid'] = False
        return JsonResponse(data)

    def get(self, request):
        data = dict()
        article_create_form = ArticleForm()
        context = {
            'article_create_form': article_create_form
        }
        data['html_article_create_form'] = render_to_string(
            'article/create_article.html',
            context,
            request=request
        )
        return JsonResponse(data)

CBV with FormView:带有 FormView 的 CBV:

class ArticleCreateView(FormView):
    template_name = 'article/create_article.html'
    form_class = ArticleForm
    form_dict = {
        'Article_create_form': ArticleForm
    }

    def get(self, request):
        data = dict()
        context = {'article_create_form': ArticleForm()}
        data['html_article_create_form'] = render_to_string(
            'article/create_article.html', context, request=request
        )
        return JsonResponse(data)

    def form_valid(self, form):
        data = dict()
        self.object = form.save()
        context = {'articles': Article.objects.all()}
        data['html_articles'] = render_to_string(
            'article/articles.html',
            context
        )
        return JsonResponse(data)

EDIT Article FormView code:编辑文章 FormView 代码:

class ArticleEditView(FormView):
    template_name = 'article/edit_article.html'
    form_class = ArticleForm

    def get(self, request, pk):
        data = dict()
        context = {
            'article': Article.objects.get(pk=pk),
            'article_edit_form': ArticleForm(instance=Article.objects.get(pk=pk))
        }
        data['html_article_edit_form'] = render_to_string(
            'article/edit_article.html', context, request=request
        )
        return JsonResponse(data)

    def form_valid(self, form):
        form.save()
        data = dict()
        data['form_is_valid'] = True
        context = {'articles': Article.objects.all()}
        data['html_articles'] = render_to_string('article/articles.html', context)
        return JsonResponse(data)

ERROR:错误:

Traceback (most recent call last):
  File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 215, in _get_response
    response = response.render()
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/response.py", line 107, in render
    self.content = self.rendered_content
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/response.py", line 84, in rendered_content
    content = template.render(context, self._request)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/base.py", line 207, in render
    return self._render(context)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/template/defaulttags.py", line 458, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/srv/envs/py27/lib/python2.7/site-packages/django/urls/base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/srv/envs/py27/lib/python2.7/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
    raise NoReverseMatch(msg)
NoReverseMatch: Reverse for 'article_edit' with arguments '('',)' not found. 1 pattern(s) tried: [u'administration/article/(?P<pk>\\d+)/edit/$']

articles.html:文章.html:

{% for article in articles %}
<div class="list-group-item" data-id='{{ article.id }}'>
   <button class="btn slideEditBtn" data-url="{% url 'article:article_edit' pk=slide.id %}">
        <i class="fa fa-pencil" aria-hidden="true"></i>
    </button>
</div>
{% endfor %}

This problem is not in any way related to your view.这个问题与您的观点没有任何关系。 The fact that you are sending json responses implies you are using Ajax here;您发送 json 响应这一事实意味着您在这里使用了 Ajax; that means you have explicitly taken responsibility from changing the page away from the browser.这意味着您已明确承担将页面从浏览器中更改的责任。

It is your Ajax script that needs to do something on successful submission.您的 Ajax 脚本需要在成功提交时执行某些操作。

you forgot to add form_is_valid to the data您忘记将form_is_valid添加到data

def form_valid(self, form):
    data = dict()
    self.object = form.save()
    context = {'articles': Article.objects.all()}
    data['html_articles'] = render_to_string(
        'article/articles.html',
        context
    )
    # NEXT LINE
    data['form_is_valid'] = True
    return JsonResponse(data)

When your form is valid, you send back the JsonResponse of the data.当您的表单有效时,您发送回数据的 JsonResponse。 What you want is to redirect the user to a page with some message.您想要的是将用户重定向到带有一些消息的页面。

Use the redirect!使用重定向! https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#redirect https://docs.djangoproject.com/en/1.11/topics/http/shortcuts/#redirect

Somethings simple like:一些简单的东西,比如:

return redirect('form-thankyou')

And of course add an urlpattern with an url with the name form-thankyou当然,添加一个带有名称为form-thankyou的 URL 的 urlpattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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