After completing the section 'Allowing Users to Enter Data (p410-414)', when I attempt to add a new topic using the field, I get an attribute error from the new_topic page.
'TopicForm' object has no attribute 'is_vaild'
I restarted the server but same issue. I've reviewed my code but can't find the bug. I did some research on the Django is_valid() attribute for ModelForm, but didn't see anything that would cause this issue. I'm running macOS Big Sur, terminal with bash, python v 3.7 and Django v 2.2. I'm not sure exactly which files to look at at this point, but here are the ones I'm guessing you want to look at.
I ran through all my code and couldn't find the problem. I actually started over from the beginning, when I got to this point, I had the same issue.
forms.py
from django import forms
from .models import Topic
class TopicForm(forms.ModelForm):
class Meta:
model = Topic
fields = ['text']
labels = {'text': ''}
urls.py
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
# Page that shoes all topics.
path('topics/', views.topics, name='topics'),
# Detail page for a single topic.
path('topics/<int:topic_id>/', views.topic, name='topic'),
# Page for adding a new topic
path('new_topic/', views.new_topic, name='new_topic'),
]
views.py
from django.shortcuts import render, redirect
from .models import Topic
from .forms import TopicForm
def index(request):
"""The home page for Learning Log."""
return render(request, 'learning_logs/index.html')
def topics(request):
"""Show all topics."""
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
def topic(request, topic_id):
"""Show a single topic and all its entries."""
topic = Topic.objects.get(id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic' : topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
def new_topic(request):
"""Add a new topic."""
if request.method != 'POST':
# No data submitted; create a blank form
form = TopicForm()
else:
# POST data submitted; process data.
form = TopicForm(data=request.POST)
if form.is_vaild():
form.save()
return redirect('learning_logs:topics')
# Display a blank or invalid form.
context = {'form': form}
return render(request, 'learning_logs/new_topic.html', context)
new_topic.html
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Add a new topic</p>
<form action="{% url 'learning_logs:new_topic' %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add topic</button>
</form>
{% endblock content %}
topics.html
{% extends 'learning_logs/base.html' %}
{% block content %}
<p>Topic: {{ topic }}</p>
<p>Entries</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>There are no entries for this topic yet.</li>
{% endfor %}
</ul>
{% endblock content %}