I am trying to build an inventory management with Django.
When building delete option, I am encountering an error.
The error message is "Reverse for 'delete' not found. 'delete' is not a valid view function or pattern name."
I will paste my code below.
Delete function code:
def delete(request, iid):
obj = inventory.objects.get(id=iid)
obj.delete()
return render(request, 'main/lists.html')
HTML Template code:
<a class="btn btn-danger bg-gradient" href="{% url 'delete' i.id %}" role="button">Delete</a>
Urls.py code:
urlpatterns = [
path('', views.index, name="index"),
path('lists', views.lists, name="lists"),
path('add', views.add, name="add"),
path('edit', views.edit, name="edit"),
path('delete/<int:id>', views.delete, name="delet") ]
Please give me a valid solution.
In urls.py the url pointing to the method is called delet.
Update it to delete on urls.py or to delet on the HTML template.
From the docs you can do this:
{% url 'delet' i.id as the_url %}
<a class="btn btn-danger bg-gradient" href="{{ the_url }}" role="button">Delete</a>
Related
I know similar kind of question is asked before, but I was not able to get it. This is my first project as a Django beginner.
In my Django blog app, I made a delete button but it is not working and I am finding for answers, trying different methods on the web but it did not help.
I am trying to do is when admin open the post, then on clicking the delete button, it take the post-id and delete that post and redirect to home page, but it is not working as expected. So, lastly I came here. Any help will be appreciated. Thanks!
This is my urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('post/<int:pk>', views.post, name='post'),
path('about', views.about, name='about'),
path('contact_us', views.contact_us, name='contact_us'),
path('register', views.register, name='register'),
path('login', views.login, name='login'),
path('logout', views.logout, name='logout'),
path('create_post', views.create_post, name='create_post'),
path('delete_post', views.delete_post, name='delete_post')
]
This is my views.py file:
def delete_post(request, *args, **kwargs):
pk = kwargs.get('pk')
post = get_object_or_404(Post, pk=pk)
if request.method == 'POST':
post.delete()
return redirect('/')
return render(request, 'delete-post.html')
This is delete post html form:
<form action="{% url 'delete_post' post.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Delete post">
</form>
Delete button:
<button type="button" class="btn btn-danger" style="position:relative; right: -1145px;">Delete</button>
for deleting a post
def delete_post(request, id):
post = Post.objects.filter(id=id)
address.delete()
return redirect('/')
and in your html
<a class="btn btn-outline-danger" href="{% url 'appname:delete_post' id=post.id %}">Delete It</a>
and in your urls.py
path('<int:id>/delete-post',views.delete_post,name='delete_post')
Template :
<a class="btn btn-primary" href="{% url 'edit' %}">Edit</a>รน
views.py:
def edit(request):
return render(request, "edit.html")
urls.py:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/create", views.create, name="create"),
path("wiki/edit", views.edit, name="edit"),
path("wiki/<str:name>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("save", views.save, name="save"),
path("random", views.random, name="random"),
]
I would like simply to switch from an HTML page on another, but the function gives me this error:
TemplateDoesNotExist at /wiki/edit
edit.html
But the template exist, I created it. I tryed a lot of changes but all of them gives me error. Thank you.
The Application name needs to be referenced in the template name.
Like so:
return render(request, "encyclopedia/edit.html")
Im new to Django (also to stackoverflow too) and im trying to make simple accounts system.
When im "sign out" button to main page im getting this error
Reverse for 'logout' not found. 'logout' is not a valid view function or pattern name.
account/views.py
def signout_view(request):
if request.method == 'POST':
logout(request)
return redirect('/')
account/urls.py
app_name = 'account'
urlpatterns = [
path('signup/', views.signup_view, name='signup'),
path('signin/', views.signin_view, name='signin'),
path('signout/', views.signout_view, name='signout'),
]
home/templates/home/wrapper.html
<form class="logout-link" action="{% url 'account:logout' %}" method="post">
{% csrf_token %}
<button type="submit">Sign out</button>
</form>
anyone can help with fixing this problem?
reverse function gets the name stated at the end of the path funtion in urls.py and retrieves its url
path('signup/', views.signup_view, name='signup'),
here the name is 'signup' and reverse('signup') will return a 'signup/'. Jinja2's {% url %} tag just calls reverse in django
so to make thing work in your code
just do
<form class="logout-link" action="{% url 'account:signout' %}" method="post">
{% csrf_token %}
<button type="submit">Sign out</button>
</form>
I'm trying to click on a button in http://127.0.0.1:8000/main/electronics/switch/ to call getCommodityCommentDetail to do something, and then redirect to another page commodityInfoPage.
What puzzles me is that the page always shows the same content in the initial page, although the url has changed e.g. to http://127.0.0.1:8000/main/comments/1/.
After testing, I found that the commodityInfoPage in views.py isn't called. I have searched long time for solutions, but all of them failed. So how can I fix it?
urls.py:
app_name = 'main'
urlpatterns = [
# eg:127.0.0.1:8000/main/
path('', views.index, name = 'index'),
path('getCommodityInfo/', views.getCommodityInfo, name = 'getCommodityInfo'),
path('getCommodityCommentDetail/', views.getCommodityCommentDetail, name="getCommodityCommentDetail"),
path('<str:category>/<str:searchKey>/',views.commodityInfoPage, name = 'commodityInfoPage'),
path('comments/<str:commodityId>/', views.commodityCommentPage,name = 'commodityCommentPage'),
]
view.py:
def getCommodityCommentDetail(request):
if request.method=="POST":
commodityId = request.POST.get("commodityId")
# scrapy module is waiting implementation
#
return HttpResponseRedirect(reverse('main:commodityInfoPage',args=(commodityId)))
def commodityCommentPage(request, commodityId):
print("enter commodityCommentPage")
commentList = JDCommentDetail.objects.all()
context = {'commentList':commentList}
return render(request,'main/commodityCommentPage.html',context)
templates:
<form action="{% url 'main:getCommodityCommentDetail'%}" method="POST">
{% csrf_token %}
<input class="hidden" value="{{commodity.id}}" name="commodityId">
<button type="submit" class="btn btn-default" >review</button>
</form>
The problem is that comments/1/ is matched by the commodityInfoPage URL pattern.
path('<str:category>/<str:searchKey>/',views.commodityInfoPage, name='commodityInfoPage'),
You can fix this problem by changing the URL patterns so that they don't clash, or by moving the commodityCommentPage URL pattern above the commodityInfoPage one.
path('comments/<str:commodityId>/', views.commodityCommentPage, name='commodityCommentPage'),
path('<str:category>/<str:searchKey>/', views.commodityInfoPage, name='commodityInfoPage'),
Note that if you re-order the patterns, you won't be able to view commodityInfoPage if the category is 'comments'.
When I click "Logout" button,Page not found error is raised.
views.py:
#login_required
def logout_view(request):
logout(request)
return render(request, 'registration/accounts/top.html')
in index.html
<div class="container">
<ul class="top-menu">
<form name="logout" method="post" action="{% url 'accounts:logout_view' %}">
<button type="submit" class="btn btn-primary btn-lg" style="color:white;background-color: #F62459;border-style: none;">Logout</button>
<input name="next" type="hidden"/>
</form>
</ul>
</div>
But I put Logout button,Page not found (404) error happens.Traceback is
Page not found (404)Request Method: POST
Request URL: http://localhost:8000/static/accounts/logout_view
My ideal system is when I put logout button, it sends top.html.
project tree
accounts
|migrations
|static
|templates
|registration
|accounts
|base.html
|detail.html
What is wrong in my code?How should I fix this?
urls.py is
urlpatterns = [
url(r'^top$', views.top,name='top'),
url(r'^detail$', views.detail,name='detail'),
url(r'^login/$', login,
{'template_name': 'registration/accounts/login.html'},
name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
]
You name the logout url 'logout':
url(r'^logout/$', views.logout_view, name='logout'),
Therefore you should also refer to it as 'logout' when reversing the url:
<form name="logout" method="post" action="{% url 'accounts:logout' %}">