編集:ユーザーがユーザーを送信して検索するときに新しいリストを使用しましたが、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})
Djangoでクラスベースのビューがどのように機能するかを理解しようとしています。で次の作業コードがありView
ます。の助けを借りてそのコードを書きたいと思いますFormView
。私の `FormView コードは正しいですか? そのコードを分析して、私の間違いがどこにあるかを教えてくれる人が必要です。どんな助けにも感謝します。
今すぐget method
動作します。問題は、submit
形成しようとするときです。フォームが消えません。同時にコンソールに次の URL が表示されます: "POST /url/ HTTP/1.1" 200 1891
. データベースで、新しい記事が作成されたことがわかります。でこの奇妙な問題を修正するにはpost method
?
ビュー付き 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)
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)
編集記事 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)
エラー:
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/$']
記事.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 %}