我CBV with View
完美无缺。我试图用FormView
重写此代码。我的FormView
代码是否正确?我需要能够分析代码的人,并说出我的错误在哪里。我将不胜感激任何帮助。
现在get method
有效。我可以打开编辑表单并查看当前数据。问题是当我尝试在更改后提交编辑表单时。似乎form_valid method
不正确。接下来你可以看到错误。
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)
使用FormView的CBV:
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'),
]
错误:
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/$']