Hussein El-Hewehii
Hussein El-Hewehii

Reputation: 74

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

enter image description herethe blog_post.html is working properly until the "edit post" link is passed , generating the error above , on the other hand it works if i removed the "blog_id" argument from the url pattern and "blog.id" from the "edit post" link ,but accordingly another error occurs when i click the "edit post" link and open the edit_post.html page it demands the missing (edit_post) argument , if i re-pass the argument again then the url patterns are not matching.

from django.conf.urls import url

from . import views

app_name = 'blogs'

urlpatterns = [
    #index
    url(r'^$',views.index,name='index'),

    #make page for blogposts
    url(r'^blogposts/$',views.blogposts,name='blog_post'),

    #page for making a new post
    url(r'^new_post/$',views.make_post,name='add_post'),

    #page for editing post
    url(r'^edit_post//(?P<blog_id>\d+)$', views.edit_post, name = 'edit_post'),
]

the view page
    from django.shortcuts import render
from django.urls import reverse
from django.http import HttpResponseRedirect
from .models import BlogPost
from .forms import BlogPostForm
def edit_post(request,blog_id):
    post = BlogPost.objects.get(id=blog_id)

    if request.method != 'POST':
        form = BlogPostForm(instance = post)
    else:
        form = BlogPostForm(instance = post, data =request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('blogs:blog_post'))
    context = {'post':post,'form':form}
    return render(request,'blogy/edit_post.html',context)



the page of the posts
 {% extends "blogy/base.html" %}

{% block content %}
<p>The Posts:</p>
<ul>
    {% for blog in blogs %}
        <li>
            <p>{{ blog.date_added|date:'M d, Y H:i' }}</p>
            <p>{{ blog.text|linebreaks }}</p>
            <p>
                <a href = "{% url 'blogs:edit_post' %}">edit post</a>
            </p>
        </li>
        {% empty %}
        <li>no posts !</li>
    {% endfor %}
</ul>
<a href="{% url 'blogs:add_post' %}">Add a new Post</a>
{% endblock content %}


the page where the edit post link goes
    {% extends "blogy/base.html" %}

{% block content %}
<p>Edit Post:</p>
<form action = "{% url 'blogs:edit_post' blog.id %}" method = 'post'>
    {% csrf_token %}
    {{ form.as_p }}
    <button name='submit'>Save Changes</button>
</form>
{% endblock content %}

Upvotes: 0

Views: 86

Answers (2)

Hussein El-Hewehii
Hussein El-Hewehii

Reputation: 74

ok i realized the problem , it should have been (post.id) to be passed not (blog.id) because the name of the variable in the view function was rendered to the html page from the dictionary "context" with the name "post" not "blog"

Upvotes: 0

Sergey Pugach
Sergey Pugach

Reputation: 5669

The issue is that your url is waiting you to pass blog_id variable, but you pass nothing. Here add blog.id

<a href = "{% url 'blogs:edit_post' blog_id=blog.id %}">edit post</a>

Upvotes: 1

Related Questions