Django slug link edit page not found

499 views Asked by At

I'm having trouble, when I use slug for my url. it said no post matches the query.

The troubles occuring in edit and delete page, it turns out still working fine within the detail page.

But the weird thing is the 404 error was raised by post.views.post_detail, not post_update.

my urls inside the post app

urlpatterns = [

url(r'r^$', post_list, name='list'),
url(r'^create/$', post_create, name='create'),
url(r'^(?P<slg>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slg>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slg>[\w-]+)/delete/$', post_delete),

]

the views

def post_detail(request, slg=None):
    instance = get_object_or_404(Post, slg=slg)
    context = {
    "title":intance.title,
    "instance":instance
    }
    return render(request, "post_detail.html", context)

def post_update(request, slg=None):
    instance = get_object_or_404(Post, slg=slg)
    form = PostForm(request.POST or None, request.FILES or None, instance=instance)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "title":instance.title,
        "instance":instance,
        "form", form,
    }
    return render(request, "post_form.html", context)

the get_absolute_url function also already return slg. the only problem is the post not found in edit page, even the slg title has a match with the current available posts.

System check identified no issues (0 silenced).
December 23, 2016 - 17:52:00
Django version 1.9.7, using settings 'blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Not Found: /hahaha/edit/
[23/Dec/2016 17:52:08] "GET /hahaha/edit/ HTTP/1.1" 404 1725

it's like the post_update function won't execute.

what could possibly be wrong ? thank you.

1

There are 1 answers

1
Moinuddin Quadri On BEST ANSWER

You are missing single quote ' in your urls. url patterns should be valid str

url(r'r^$', post_list, name='list'),
url(r'^create/$', post_create, name='create'),
#              ^
url(r'^(?P<slg>[\w-]+)/$', post_detail, name='detail'),
#                       ^
url(r'^(?P<slg>[\w-]+)/edit/$', post_edit, name='edit'), 
#                            ^
url(r'^(?P<slg>[\w-]+)/delete/$', post_delete),
#                              ^