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’
- [Answered ]-Generate PDF of Protected Django Webpage with Attachments
- [Answered ]-Passing kwargs into get_or_create method in Django
- [Answered ]-Django and formsets
- [Answered ]-Adding Additional Hallo.js formatting features to Wagtail CMS RichTextField
- [Answered ]-Empty Chatterbot Conversation table in Django Admin
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.
Source:stackexchange.com