Rafael
Rafael

Reputation: 23

Django Page not found Request Method: GET

I can't seem to figure out why my page can't be found. All the others previous ones are written exactly the same way. Why is this happening?

VIEWS:

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)

URLS:

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 %}

Upvotes: 0

Views: 83

Answers (2)

Surya hangam
Surya hangam

Reputation: 21

Also change

return redirect('learning_logg:topic', topic_id=topic_id)

To

return redirect('learning_logg:topics', kwargs={'topic_id':topic.id})

Upvotes: 0

Surya hangam
Surya hangam

Reputation: 21

The issue lies in your new_entry.html. Update your second last line to

<p><a> href="{% url 'learning_logg:topics' topic.id %}">{{ topic }}</a></p>

Instead of

<p><a> href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p>

Upvotes: 1

Related Questions