Answer the question
In order to leave comments, you need to log in
FormView for edit form?
Hello! Please help me figure it out.
I'm trying to rewrite the view code from CBV(View) to CBV(FormView) . CBV(View) works great, but CBV(FormView) throws an error when I click the submit button on the edit form. The get method works correctly, I get the edit form with the actual data. I think the problem is either in the form_valid method or in the fact that I forgot to write something additionally in CBV (FormView). Where did I make a mistake?
CBV(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)
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)
{% 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 %}
urlpatterns = [
# Edit Article
url(r'^(?P<pk>\d+)/edit/$',
ArticleEditView.as_view(),
name='article_edit'),
]
$("#articleModalBox").on("submit", ".articleEditForm", function(){
var form = $(this);
var dataForm = new FormData(form.get(0));
$.ajax({
url: form.attr("action"),
data: dataForm,
type: form.attr("method"),
dataType: 'json',
success: function (data) {
if (data.form_is_valid) {
$("#articles").html(data.html_articles);
$("#articleModalBox").modal("hide");
}
else {
$("#articleModalBox .modal-content").html(data.html_article_edit_form);
}
},
cache: false,
contentType: false,
processData: false
});
return false;
});
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/$']
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question