FormView in edit form | Django dont see object pk?

373 views Asked by At

I have CBV with View which works perfect. I am trying to rewrite this code with 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. I can open edit form and see currect data. The problem is when I try to submit edit form after changes. It seems like form_valid method not correct. Next below you can see error.

CBV with View:

class ArticleEditView(View):
    def post(self, request, pk, *args, **kwargs):
        data = dict()
        article = Article.objects.get(pk=pk)
        article_edit_form = ArticleForm(
            request.POST,
            request.FILES,
            instance=article
        )
        if article_edit_form.is_valid():
            article_edit_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, pk, *args, **kwargs):
        data = dict()
        article = Article.objects.get(pk=pk)
        article_edit_form = ArticleForm(instance=article)
        context = {
            'article': article,
            'article_edit_form': article_edit_form
        }
        data['html_article_edit_form'] = render_to_string(
            'article/edit_article.html',
            context,
            request=request
        )
        return JsonResponse(data)

CBV with 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)

articles.html:

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

urls.py:

urlpatterns = [
    # Edit Article
    url(r'^(?P<pk>\d+)/edit/$',
        ArticleEditView.as_view(),
        name='article_edit'),
]

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/$']
0

There are 0 answers