我是初学Django的新手,并且我想使用Django学习追踪器网络应用程序。当我为新条目创建函数和模板时出现错误-这将允许用户编写有关他们当前正在学习的主题的详细注释,但是每当我运行模板时,都会收到错误消息:
找不到带有参数“('',)”的“ new_entry”。尝试了1个模式:['new_entry /(?P [0-9] +)/ $']。
我尝试查看我编写的最新new_entry()函数,并调整主题变量名,但是并不能解决问题。我还交叉检查了我的url路径是否存在任何拼写错误或空格,但没有。这是我的项目文件。
urls.py
from django.urls import path
from . import views
app_name = 'django_apps'
urlpatterns = [
# Home page
path('', views.index, name='index'),
# Page that shows 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'),
# Page for adding a new entry.
path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
]
views.py:
from django.shortcuts import render, redirect
from .models import Topic
from .forms import TopicForm, EntryForm
# Create your views here.
def index(request):
"""The home page for django app."""
return render(request, 'django_apps/index.html')
def topics(request):
"""Show all topic"""
topics_list = Topic.objects.order_by('id')
context = {'topics_list': topics_list}
return render(request, 'django_apps/topics.html', context)
def topic(request, topic_id):
"""Get topic and all entries associated with it."""
topic_list = Topic.objects.get(id=topic_id)
entries = topic_list.entry_set.order_by('-date_added')
context = {'topic_list': topic_list, 'entries': entries}
return render(request, 'django_apps/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_valid():
form.save()
return redirect('django_apps:topics')
# Display a blank name or invalid form.
context = {'form': form}
return render(request, 'django_apps/new_topic.html', context)
def new_entry(request, topic_id):
"""Add a new entry for a topic."""
topic_list = Topic.objects.get(id=topic_id)
if request.method != 'POST':
# NO data submitted; create a blank form.
form = EntryForm()
else:
# POST data submitted; process data.
form = EntryForm(data=request.POST)
if form.is_valid():
latest_entry = form.save(commit=False)
latest_entry.topic = topic_list
latest_entry.save()
return redirect('django_apps:topic', topic_id=topic_id)
# Display a blank name or invalid form.
context = {'topic_list': topic_list, 'form': form}
return render(request, 'django_apps/new_entry.html', context)
new_entry.html(已更新!):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Tracker - Entry</title>
</head>
<body>
{% extends 'django_apps/base.html' %}
{% block content %}
{% for topic in topic_list %}
<p><a href="{% url 'django_apps:topic' topic_id=topic.id %}">{{ topic }}</a></p>
<p>Add a new entry:</p>
<form action="{% url 'django_apps:new_entry' topic_id=topic.id %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add entry</button>
</form>
{% endfor %}
{% endblock content %}
</body>
</html>
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base template</title>
</head>
<body>
<p>
<a href="{% url 'django_apps:index' %}">Study Tracker</a> -
<a href="{% url 'django_apps:topics' %}">Topics</a>
</p>
{% block content %}{% endblock content %}
</body>
</html>
forms.py:
from django import forms
from .models import Topic, Entry
class TopicForm(forms.ModelForm):
"""A class that defines the form in which a user can enter in a topic."""
class Meta:
"""This class tells django which model to base the form on and the
fields to include in the form."""
model = Topic
fields = ['text']
labels = {'text': ''}
class EntryForm(forms.ModelForm):
"""A class that defines the form in which a user can fill in an entry to
a topic."""
class Meta:
"""This meta class tells django which model to base the form for
entries on and the fields to include in the form."""
model = Entry
fields = ['text']
labels = {'text': 'Entry:'}
widgets = {'text': forms.Textarea(attrs={'cols': 80})}
我不知道如何解决此问题。我检查了我所有的继承URL和模板是否有错误,但是我无法弄清楚问题出在哪里。
更新:这是我的topic.html模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Tracker - Topic</title>
</head>
<body>
{% extends 'django_apps/base.html' %}
{% block content %}
<p>Topic: {{ topic }}</p>
<p>Entries:</p>
<p>
<a href="{% url 'django_apps:new_entry' topic.id %}">Add new entry</a>
</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 currently no entries for this topic.</li>
{% endfor %}
</ul>
{% endblock content %}
</body>
</html>
您传递了模板topic_list变量,但在模板中使用了topic。我想您会设置一个循环。因为您没有任何名为topic的变量。如果您更改了它们,它将起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句