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>
Related
In my Django Rest Framework, the users request to reset the password and when the email is received and the link is clicked, the url
password-reset-confirm/<uidb64>/<token>/ as comes up requested but the form is not showing and when I added it as {{ form }} is displayed NONE
The password reset process is working perfectly fine when I do everytihng on the Django but if I try to reset the password from Django
Rest Framework the form does not appear.
Here is the main urls.py
urlpatterns = [
path('', include('django.contrib.auth.urls')),
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html', success_url=reverse_lazy('password_reset_done')), name='password_reset'),
path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),name='password_reset_confirm',),
path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'), name='password_reset_complete'),
path('admin/', admin.site.urls),
path('api/', include('api.urls'), ),
path('users/', include('users.urls'), ),
]
Here is the API app urls.py that is related to DRF
app_name = 'api'
router = routers.DefaultRouter()
router.register(r'users', UserViewSet, basename='user')
urlpatterns = [
path('', include(router.urls)),
path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
here is the template password_reset_confirm.html
<main class="mt-5" >
<div class="container dark-grey-text mt-5">
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Reset Password</legend>
{{ form|crispy }}
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Reset Password</button>
</div>
</form>
</div>
</div>
</main>
My question is: Why is the form showing as NONE and how do I fix it.
Most probably the form is not going to password_reset_confirm.html since django-rest-framework does not include the form in the context of the PasswordResetConfirmView by default.
Currently, the thing you can do is to create a custom PasswordResetConfirmView by inheriting it in a sub class and pass the form to the template context, using form_class attribute so:
from django.contrib.auth.forms import SetPasswordForm
from django.contrib.auth.views import PasswordResetConfirmView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters
class CustomPasswordResetConfirmView(PasswordResetConfirmView):
form_class = SetPasswordForm
success_url = reverse_lazy('password_reset_complete')
template_name = 'users/password_reset_confirm.html'
#method_decorator(sensitive_post_parameters('new_password1', 'new_password2'))
#method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = self.form_class(user=self.request.user)
return context
Then in urls.py:
urlpatterns = [
# .......
path('password-reset-confirm/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
# .......
]
You can provide any success_url according to your need.
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),
]
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})
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')