python - 找不到 Django 页面请求方法:GET
问题描述
我似乎无法弄清楚为什么找不到我的页面。所有其他以前的都以完全相同的方式编写。为什么会这样?
意见:
def new_entry(request, topic_id):
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
form = EntryForm()
else:
form = EntryForm(data=request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.topic = topic
new_entry.save()
return redirect('learning_logg:topic', topic_id=topic_id)
context = {'topic': topic, 'form': form}
return render(request, 'learning_logs/new_entry.html', context)
网址:
app_name = "learning_logg"
urlpatterns = [
#Home Page
path('', views.index, name="index"),
path('topics/', views.topics, name="topics"),
path('topics/<int:topic_id>/', views.topics, name="topics"),
path('new_topic/', views.new_topic, name='new_topic'),
path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
]
NEW_ENTRY.HTML:
{% extends "learning_logs/base.html" %}
{% block content %}
<p>Add a new entry:</p>
<form action="{% url 'learning_logg:new_entry' topic.id %}" method='post'>
{% csrf_token %}
{{ form.as_pp }}
<button name='submit'>Add entry</button>
</form>
<p><a> href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p>
{% endblock content %}
解决方案
问题在于您的 new_entry.html。将您的倒数第二行更新为
<p><a> href="{% url 'learning_logg:topics' topic.id %}">{{ topic }}</a></p>
代替
<p><a> href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p>
推荐阅读
- python - gzip.open().read() 的大小参数
- ibm-mobilefirst - 在 MobileFirst Server 8 上注册应用程序时平台不可见
- botframework - 在 SDK-4.0 中创建表单流
- ruby-on-rails - Rails (5.2.2) AJAX 调用部分不工作
- android - 如何获取json数组
- excel - Excel VBA粘贴从活动单元格到合并单元格的偏移量
- c++ - 在 C-struct 中查找相同的元素
- javascript - react-admin 通过自定义表单刷新 dataGrid
- mongodb - 如何在 MongoDB 中使用 map reduce 求平均值?
- r - 如何在ggplot中修改x轴上的时间?