Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse for 'edit_post' with arguments '('',)' not found. 1 pattern(s) tried: ['edit_post/(?P<post_id>\\d+)/$']

Tags:

python

django

I am going through a Django tutorial and getting this error when trying to edit posts in my blog app. I use Django Version: 2.0.6 and Python Version: 3.6.5.

models.py

from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()

    def __str__(self):
        return self.title

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^new_post/', views.new_post, name='new_post'),
    url(r'^edit_post/(?P<post_id>\d+)/$', views.edit_post, name='edit_post'),
]

A template that causes the error in line 3 - edit_post.html. The error message highlights {% url 'edit_post' post.id %}

{% block content %}
  <form action="{% url 'edit_post' post.id %}" method='post'>
    {% csrf_token %}
    {{ form.as_p }}
    <button name="submit">save changes</button>
  </form>
{% endblock content %}

A template (index.html) with a link to edit_post.html

{% block content %}
  <form action="{% url 'new_post' %}" method='post'>
    {% csrf_token %}
    {{ form.as_p }}
    <button name="submit">Make a new post</button>
  </form>
  <ul>
    {% for post in posts %}
      <li>
        {{ post.id }} - {{ post.title }}
        <p>{{ post.text }}</p>
        <a href="{% url 'edit_post' post.id %}">edit post</a>
      </li>
    {% empty %}
      <li>No posts have been added yet.</li>
    {% endfor %}
  </ul>
{% endblock content %}

views.py

def edit_post(request, post_id):
    post = BlogPost.objects.get(id=post_id)
    text = post.text
    title = post.title
    if request.method != 'POST':
        form = BlogForm(instance=post)
    else:
        form = BlogForm(instance=post, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('index'))
    context = {'title': title, 'text': text, 'form': form}
    return render(request, 'blog/edit_post.html', context)

forms.py

from django import forms
from .models import BlogPost

class BlogForm(forms.ModelForm):
    class Meta:
        model = BlogPost
        fields = ['title', 'text']

The problem

When I click edit post link on index page, I get the aforementioned error. Creating a new post using this approach works flawlessly but editing does not. I am stuck with this problem and have no idea what is wrong.

What I have tried

  1. I have tried replacing django.conf.urls.url with django.urls.path and the patterns correspondingly.
  2. I have change a link to a button.
  3. I have tried
  4. I have read What is a NoReverseMatch error, and how do I fix it? on StackOverflow and as many topics as I could find.

Any help is appreciated. Thank you in advance!

like image 907
Jim Doe Avatar asked Jun 12 '18 06:06

Jim Doe


1 Answers

in your blog/edit_post.html you use post.id

<form action="{% url 'edit_post' post.id %}" method='post'>
    ...
</form>

but in views.py you don't pass post variable to context variable

def edit_post(request, post_id):
    post = BlogPost.objects.get(id=post_id)
    ...

    context = {
              'title': title, 
              'text': text, 
              'form': form,
              'post': post # here
              }
    return render(request, 'blog/edit_post.html', context)
like image 147
Druta Ruslan Avatar answered Nov 03 '22 00:11

Druta Ruslan