i created a view to delete an object through a template but it seems im missing something i cant quite put together
def product_delete_view(request, id):
obj = get_object_or_404(Product, id=id)
if request.method == "POST":
obj.delete()
return redirect('../../')
context = {
"obj": obj
}
return render(request, "products/product_delete.html", context)
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view, social_view, services_view
#from products.views import product_detail_view, product_create_view, render_intial_data
from products.views import render_intial_data, dynamic_lookup_view, product_delete_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
path('contact/', contact_view, name='contact'),
path('about/', about_view, name='about'),
path('services/', services_view, name='services'),
path('social/', social_view, name='social'),
#path('product/detail', product_detail_view, name='pdtDetail'),
#path('product/create', product_create_view, name='pdtCreate')
path('product/create', render_intial_data, name='pdtCreate'),
path('product/<int:id>', dynamic_lookup_view, name="pdt"),
path('product/<int:id>/delete', product_delete_view, name='pdtDelete')
]
{% extends 'base.html' %} {% block content %}
<form action="." method="POST">
{% csrf_token %}
<h1>Do you want to delete the product "{{ obj.title }}"</h1>
<p><input type="submit" value="Yes" /> Cancel</p>
</form>
{% endblock %}
when i try to delete object is not removed from the database table
Your patterns doesn't ending with slash. Try to add ending slash to it:
path('product/create/', render_intial_data, name='pdtCreate'),
path('product/<int:id>/', dynamic_lookup_view, name="pdt"),
path('product/<int:id>/delete/', product_delete_view, name='pdtDelete')
In your urls.py there is something like this product/<int:id>/delete and you are typing produck/4/ in your browser. use this http://127.0.0.1:8000/product/4/delete and use '/' at last like this http://127.0.0.1:8000/product/4/delete/
your problem seems here
path('product/<int:id>/delete', product_delete_view, name='pdtDelete')
you had left that url without closing with /. just do this
path('product/<int:id>/delete/', product_delete_view, name='pdtDelete')
Related
The Form in register.html request for POST method but GET method is working. Submit is calling register method in views.py using GET method, but this shouldn't be happening.
Error :
Request Method: POST
Request URL: http://127.0.0.1:8000/Profile/register/register/
views.py
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.contrib.auth.models import User
from django.contrib import messages
def register(request):
if request.method == 'POST':
return redirect('/')
else:
return render(request, 'register.html')
register.html
<form action="register" method="post">
{% csrf_token %}
<input type="Submit">
</form>
urls.py of Project
from re import template
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
path('Profile/', include('Profile.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('login/', auth_views.LoginView.as_view(template_name="login.html"),
name='login'),
path('logout/', auth_views.LogoutView.as_view(),
name='logout'),
path('reset_password/', auth_views.PasswordResetView.as_view(),
name = 'reset_password'),
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete')
]
urls.py of Profile app
from django.urls import path
from Profile import views
urlpatterns = [
path('profile/', views.profile, name='profile'),
path('register/', views.register, name='register'),
]
The URL is register/. Because you do not use a trailing slash, Django will make a redirect with a slash, but that will thus result in a GET request, not a POST request.
You thus should work with:
<form action="/profile/register/" method="post">
{% csrf_token %}
<input type="Submit">
</form>
or better is to work with the {% url … %} template tag [Django-doc]:
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
<input type="Submit">
</form>
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')
I am. trying to. build a search function for my django project base on the enter link description here
and the error message popped out "Reverse for 'Search' not found. 'Search' is not a valid view function or pattern name."
i have done the searching most of the advise asking me to check if the spelling is error
like my "search" in url or my search.html in my render
however, i have tried all the solution it still can't work
here is some of my code
urls.py:
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product),
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
from .models import Product
from .forms import ProductForm, RawProductForm,VendorForm
def search_product(request):
if request.method == "POST":
query_name = request.POST.get('title', None)
if query_name:
results = Product.objects.filter(name__contains=query_name)
return render(request, 'products/search.html', {"results":results})
return render(request, 'products/search.html')
search.html
<!DOCTYPE html>
<html>
<head>
<title>Django Search</title>
</head>
<body>
<form action="{% url 'search' %}" method="POST">
{% csrf_token %}
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
{% for result in results %}
<p>{{result.name}}</p>
{% endfor %}
</body>
</html>
and following is my folder in case if needed
You need to specify the name of the urls.py with the name=… parameter:
urlpatterns = [
# …,
# name ↓
path('search/', search_product, name='search'),
# …
]
Change this file.. urls.py
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product, name = 'search'), # changed here
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
So, I have a code below:
newEra/urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path("",include('mentor.urls')),]
mentor/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home_page),
]
my views.py
from django.shortcuts import render,redirect
from .models import *
from .forms import *
def home_page(request):
todo = ToDo.objects.all()
form = ToDoForms()
if request.method == 'POST':
form = ToDoForms(request.POST)
if form.is_valid():
form.save()
return redirect('')
context = {'todo' : todo, 'form':form}
return render(request, 'home.html',context)
home.html
<h1>To-Do list</h1>
<form action="POST" method="/">
{% csrf_token %}
{{form.name}}
<input type="submit" value="Create task" >
</form>
<ul>
{% for to in todo %}
<li>{{ to }}</li>
{% endfor %}
</ul>
But I am getting this error below
**Using the URLconf defined in NewEra.urls, Django tried these URL patterns, in this order:
admin/
The current path, POST, didn't match any of these.**
'mentor' file added to the SETTINGS
URLs are regexes.
Try:
urlpatterns = [
path('^/$',views.home_page),
]
I'm trying to render index HTML and get post title from database but I'm getting error. I define in views post database but still getting error
name 'Post_title' is not defined
my app/views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import reverse
from .models import BlogPost,comments
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,{Post_title:"Post_title",})
def post_detail(request):
return render(request, 'front/post_detail.html')
my app/urls.py
from django.urls import path
from .import views
urlpatterns = [
path('', views.index, name = 'index'),
path('<int:BlogPost_id>/', views.post_detail, name='Post Detail')
]
my project/urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from froala_editor import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
my index.html template
<div class="col-md-8 mt-3 left">
{% for post in Post_list %}
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title">{{ post.Post_title }}</h2>
</div>
</div>
{% endfor %}
</div>
You are not sending the Post_list to the template through context. Send it like this
return render(request, template_name, {'Post_list':Post_list})