Hi,
add one more line in your view.py header
from django.views.decorators.csrf import csrf_exempt
then add decorator before your function
@csrf_exempt
On Tue, May 28, 2019 at 5:26 PM isorae dennis <osasisorae@gmail.com> wrote:
Did you indent accurately--On Tue, May 28, 2019, 12:32 The Aryas <arya2harsh@gmail.com> wrote:hello guys, i was working on a clone project and got stuck on a problem. the {% csrf_token %} that i have applied is not verified ...and the error login module is following>>--====================================================================================================================================Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
- Your browser is accepting cookies.
- The view function passes a
request
to the template'srender
method.- In the template, there is a
{% csrf_token %}
template tag inside each POST form that targets an internal URL.- If you are not using
CsrfViewMiddleware
, then you must usecsrf_protect
on any views that use thecsrf_token
template tag, as well as those that accept the POST data.- The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.
You're seeing the help section of this page because you have
DEBUG = True
in your Django settings file. Change that toFalse
, and only the initial error message will be displayed.You can customize this page using the CSRF_FAILURE_VIEW setting.================================================================================================================================I have applied all the requirements but still that occurs. here is my code>><login.html>{% extends 'blog/base.html' %}{% block content %}<div class="jumbotron"><h2>Please login!</h2><h3>(must be suoer user , please check with site admin)</h3></div>{% if forms.errors %}<p>Your user name and password did not match please try again!</p>{% endif %}<form action="{% url 'login' %}" method="POST">{% csrf_token %}{{ form.as_p }}<input type="submit" class="btn btn-primary" value="login"><input type="hidden" name="next" value="{{next}}"></form>{% endblock %}===================================================================================<urls.py- project(mysite)>from django.contrib import adminfrom django.http import HttpResponsefrom django.shortcuts import get_object_or_404, renderfrom django.urls import pathfrom django.conf.urls import includefrom django.contrib.auth import viewsurlpatterns = [path('admin/', admin.site.urls),path('',include('blog.urls')),path('accounts/login/',views.LoginView.as_view(), name='login'),path('accounts/logout/',views.LogoutView.as_view(), name='logout',kwargs={'next_page':'/'})]===================================================================================<views.py>from django.shortcuts import render,get_object_or_404,redirectfrom django.utils import timezonefrom blog.models import Post,Commentfrom blog.forms import PostForm,CommentFormfrom django.urls import reverse_lazyfrom django.contrib.auth.decorators import login_requiredfrom django.contrib.auth.mixins import LoginRequiredMixinfrom django.views.generic import (TemplateView,ListView,DetailView,CreateView,UpdateView,DeleteView)# Create your views here.class AboutView(TemplateView):template_name='about.html'class PostListView(ListView):model=Postdef get_queryset(self):return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')class PostDetailView(DetailView):model=Postclass CreatePostView(LoginRequiredMixin,CreateView):login_url='/login'redirect_field_name='blog/post_detail.html'form_class=PostFormmodel=Postclass PostUpdateView(LoginRequiredMixin,UpdateView):login_url='/login'redirect_field_name='blog/post_detail.html'form_class=PostFormmodel=Postclass PostDeleteView(LoginRequiredMixin,DeleteView):model=Postsuccess_url=reverse_lazy('post_list')class DraftListView(LoginRequiredMixin,ListView):login_url='/login/'redirect_field_name='blog/post_list.html'model=Postdef get_queryset(self):return Post.objects.filter(published_date_isnull=True).order_by('created_date')@login_requireddef add_comment_to_post(request,pk):post=get_object_or_404(post,pk=pk)if request.method == 'POST':form=CommentForm(request.POST)if form.is_valid():Comment=form.save(commit=False)comment.post=postcomment.save()return redirect('post_detail',pk=post.pk)else:form=CommentForm()return render(request,'blog/comment_form.html',{'form':form})@login_requireddef comment_approve(request,pk):comment=get_object_or_404(Comment,pk=pk)comment.approve()return redirect('post_detail',pk=comment.post.pk)@login_requireddef comment_remove(request,pk):comment=get_object_or_404(Comment,pk=pk)post_pk=comment.post.pkcomment.delete()return redirect('post_detail',pk=post_pk)@login_requireddef post_publish(request,pk):post=get_object_or_404(Post,pk=pk)post.publish()return redirect('post_detail',pk=pk)===========================================================================guys plz help me out to run my codethank you
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e2b9b83-7aab-46f5-867d-8de101777762%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADj5egyrOTMmWzUJinqZgfCJKhPX%3DFDQGbE3Pd%2BxN_PXGLPD1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHcYWK9VMorHbuFzSaDpm3UR%2BpED4aVSc6ZUZhxVXHPUO%2B4fZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment