前提・実現したいこと
カレンダーを表示するwebアプリを作りたいです。また、カレンダーにフォームを付けて日付や読んだ本の文字数などを登録したいです。
カレンダーを表示するまではできましたが、フォームを送信するコードを書いたら、エラーがでるようになりました。ライブラリでエラーが吐かれているので、コードのどこでエラーが出ているのかわかりません。
発生している問題・エラーメッセージ
cmd
1AttributeError: 'function' object has no attribute 'get' 2
該当のソースコード
urls.py
python3
1from django.contrib import admin 2from django.urls import include,path 3 4urlpatterns = [ 5 path('admin/', admin.site.urls), 6 path('', include('app.urls')), 7] 8
urls.py
from django.urls import path from . import views, forms app_name = 'app' urlpatterns = [ path( 'month_with_schedule/', views.MonthWithScheduleCalendar.as_view(), name='month_with_schedule' ), path( 'month_with_schedule/<int:year>/<int:month>/', views.MonthWithScheduleCalendar.as_view(), name='month_with_schedule' ), ]
base.html
<!doctype html> <html lang="ja"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> <title>カレンダー</title> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <a class="nav-link" href="{% url 'app:month_with_schedule' %}">多読カレンダー</a> </li> </ul> </div> </nav> <div class="container mt-3"> {% block content %}{% endblock %} </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script> {% block extrajs %}{% endblock %} </body> </html>
month.html
<a href="{% url 'app:mycalendar' month_previous.year month_previous.month month_previous.day %}">前月</a> {{ month_current | date:"Y年m月" }} <a href="{% url 'app:mycalendar' month_next.year month_next.month month_next.day %}">次月</a> <table class="table" style="table-layout: fixed;"> <thead> <tr> {% for w in week_names %} <th>{{ w }}</th> {% endfor %} </tr> </thead> <tbody> {% for week in month_days %} <tr> {% for day in week %} {% if now == day %} <td class="table-success"> {% else %} <td> {% endif %} <a href="{% url 'app:mycalendar' day.year day.month day.day %}">{{ day.day }}</a> </td> {% endfor %} </tr> {% endfor %} </tbody> </table>
month_with_schedule.html
{% extends 'app/base.html' %} {% block content %} <form action='' method='POST'> <div class='row'> {% for form in formset %} <div class='col-sm-4'> {{ form.as_p }} </div> {% endfor %} </div> {{ formset.management_form }} {% csrf_token %} <button type='submit' class='btn btn-primary'>送信</button> </form> {% endlock %} <style> table { table-layout: fixed; } td > div { height: 100px; overflow: hidden; white-space: nowrap; } </style> <a href="{% url 'app:month_with_schedule' month_previous.year month_previous.month %}">前月</a> {{ month_current | date:"Y年m月" }} <a href="{% url 'app:month_with_schedule' month_next.year month_next.month %}">次月</a> {{ form }} <table class="table"> <thead> <tr> {% for w in week_names %} <th>{{ w }}</th> {% endfor %} </tr> </thead> <tbody> {% for week_day_schedles in month_day_schedules %} <tr> {% for day, schedules in week_day_schedles.items %} {% if now == day %} <td class="table-success"> {% else %} <td> {% endif %} <div> {% if month_current.month != day.month %} {{ day | date:"m/d" }} {% else %} {{ day.day }} {% endif %} {% for schedule in schedules %} <p>{{ schedule.summary }}</p> {% endfor %} </div> </td> {% endfor %} </tr> {% endfor %} </tbody> </table> {% endblock %}
views.py
import datetime from django.shortcuts import redirect, render from django.views import generic from .forms import SimpleScheduleForm from .models import Schedule from . import mixins import requests class MonthWithScheduleCalendar(mixins.MonthWithScheduleMixin, generic.TemplateView): """スケジュール付きの月間カレンダーを表示するビュー""" template_name = 'month_with_schedule.html' model = Schedule date_field = 'date' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) calendar_context = self.get_month_calendar() context.update(calendar_context) return context form = SimpleScheduleForm(requests.post) is_valid = form.is_valid()
forms.py
from django import forms from .models import Schedule import requests class SimpleScheduleForm(forms.ModelForm): """シンプルなスケジュール登録用フォーム""" class Meta: model = Schedule fields = ('date', 'title', 'word_count', 'evaluation', 'impressions') widgets = { 'date': forms.HiddenInput, 'title': forms.TextInput, 'word_count': forms.NumberInput, 'evaluation': forms.NumberInput, 'impressions': forms.TextInput, } def clean(self): super().clean() date = self.cleaned_data() title = self.cleaned_data() word_count = self.cleaned_data() evaluation = self.cleaned_data() impressions = self.cleaned_data() form = SimpleScheduleForm(requests.post) info = form.save(commit=False) info.save() PostCreateFormSet = forms.modelformset_factory( requests.post, form=SimpleScheduleForm, extra=1 )
models.py
import datetime from django.db import models EVALUATION = ( (1, '◎'), (2, '○'), (3, '△'), (4, '×'), ) class Schedule(models.Model): date = models.DateField(verbose_name='日付') title = models.CharField(verbose_name='タイトル', max_length=100, blank=True) word_count = models.IntegerField(verbose_name='文字数', blank=True) evaluation = models.IntegerField(verbose_name='評価', choices=EVALUATION, blank=True) impressions = models.CharField(verbose_name='感想', max_length=255, blank=True) def __str__(self): return self.date
補足情報(FW/ツールのバージョンなど)
ubuntu18.04.3
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。