首页 > 解决方案 > 找不到 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 %}

标签: pythondjango

解决方案


问题在于您的 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>

推荐阅读