勉強をかねて簡単なブログを作成している途中のDjango初心者です。
記事の検索機能を追加している最中にMultiValueDictKeyErrorとでました。
blog/post_list.html で<input type="submit" value="Search">と指定されたserchボタンを出力画面上で押すとMultiValueDictKeyErrorの画面に飛ばされます。
このエラーの意味と下記のコードの何に原因があるかもしお気付きの点あればご教授お願い致します。
urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('find/', views.find, name='find'), ]
views.py
from django.shortcuts import redirect from django.shortcuts import render from .models import Post from .forms import PostForm from django.core.paginator import Paginator from .forms import FindForm def index(request, num=1, pk=1): posts = Post.objects.order_by('published_date').reverse() page = Paginator(posts, 3) num2 = Post.objects.all().count() params = { 'data': page.get_page(num), 'num2' : num2, '' : pk } return render(request, 'blog/post_list.html', params) def find(request, num=1): if request.method == 'POST': form = FindForm(request.POST) str = request.POST['find'] data = Post.objects.filter(name=str) num2 = data.count() page = Paginator(data, 3) params = { 'data' : page.get_page(num), 'num2' : num2, } return render(request, 'blog/post_list.html', params) else: return redirect('index')
blog/post_list.html
{% extends 'blog/base.html' %} {% block content %} <p class='artticle_num'>記事数:{{ num2 }}</p> <div> <form action="{% url 'find' %}" method="post"> {% csrf_token %}{{form}} <input type="submit" value="Search"> </form> </div> {% for post in data %} <div class="post"> <div class="date"> {{ post.published_date }} <div> 投稿者:{% if user.username == 'TARO' %}夫 {% elif user.username == 'HANAKO' %}妻 {% else %}長女 {% endif %} </div> </div> <h2><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h2> <p>{{ post.sub_title}}</p> <p>{{ post.text|linebreaksbr }}</p> </div> {% endfor %} <div class="pagination"> {% if data.has_previous %} <a href="{% url 'index' %}">«First</a> <a href="{% url 'index' %}{{data.previous_page_number}}">«Prev</a> {% endif %} <span class="current"> [{{data.number}}/{{data.paginator.num_pages}}] </span> {% if data.has_next %} <a href="{% url 'index' %}{{data.next_page_number }}">Next»</a> <a href="{% url 'index' %}{{data.paginator.num_pages }}">Last»</a> {% endif %} </div> {% endblock %}
forms.py
from django import forms from .models import Post class PostForm(forms.ModelForm): class Meta: model = Post fields = ('title', 'sub_title', 'text') class FindForm(forms.Form): find = forms.CharField(label='記事を検索する', required = False)
models.py
from django.conf import settings from django.db import models from django.utils import timezone class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) title = models.CharField(max_length=200) sub_title = models.CharField(max_length=100) text = models.TextField() created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now() self.save()
回答1件
あなたの回答
tips
プレビュー