[Answered ]-Request.method == 'POST' is not working in Django

1👍

In your case every time action go to the get_category, because django find first match urls and use it for action.

You can create one function for your case:
in views.py

def category(request):
    if request.method == 'POST':
        title = request.POST['title']
        # update start
        cat = request.POST['cat']
        cate = Category.objects.get(pk=cat)
        # Update end
        desc = request.POST['desc']
        art = article(title=title, disc=desc, cat=cate)
        art.save()
        return redirect('blog/index.html')

    get = category.objects.all()
    context = {
        'get':get
    }
    return render(request, 'blog/add.html', context)

in urls.py

urlpatterns = [
   url(r'^$', views.index, name='index'),
   url(r'^add/$', views.get_category, name='category'),
   # !! comment or remove last urls
   # url(r'^add/$', views.add, name='add'),
 ]

I edit but if you look and rebuild your logic with django forms it will be better.

1👍

<input type="submit" class="btn btn-primary" value="Add item">
Add a Submit button instead of type = ‘button’

0👍

reconfig project(makemigration and migrate) if POST method is not working

0👍

Change:

return redirect(request, 'blog/index.html')

To:

return redirect('index')

‘index’ is the name attribute of view function in views.py file. As redirect doesn’t take ‘request’ argument as parametre.

Leave a comment