POST method of FormView in Django?

7.4k views Asked by At

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)

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. I have next working code with View. I want to write that code with the help of FormView. Is my `FormView code correct? 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. The problem is when I try to submit form. Form dont disappear. In the same time in console I see this 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?

CBV with View:

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:

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:

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:

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

There are 3 answers

3
AudioBubble On BEST ANSWER

you forgot to add form_is_valid to the 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)
1
Daniel Roseman On

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; 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.

2
Karim N Gorjux On

When your form is valid, you send back the JsonResponse of the data. 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

Somethings simple like:

return redirect('form-thankyou')

And of course add an urlpattern with an url with the name form-thankyou