forbidden error in django

44 views
Skip to first unread message

DHaval Joshi

unread,
Jun 3, 2015, 7:22:37 AM6/3/15
to django...@googlegroups.com

its give a forbidden error by dhaval joshi

6.1 KB, Python 2
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's render 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 use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.

You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.

You can customize this page using the CSRF_FAILURE_VIEW setting.


this error is accurd what to do for solve it









url.py


from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mbrsgvp.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'mbrs.views.home'),
    url(r'^log/', 'mbrs.views.log'),
)


views.py


from django.shortcuts import render,HttpResponse,render_to_response
from .models import login


def home(request):
    return render_to_response('home.html')

def log(request):
 	
    q=login("123","1423")
    q.save
    return HttpResponse("success")



html template

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
  <head>
     	<link rel="stylesheet" type="text/css" href="{% static 'engine1/style.css' %}" />
</head>
<body>
<div id="navbar" class="navbar-collapse collapse">
				{% block content %}
				<form class="navbar-form navbar-right" action="log/" method="post">
					{% csrf_token %} 
					<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
					<div class="form-group form-group-sm">
						<div>
							<p style="color:white">
								
							</p>
						</div>
						<div class="input-group">
							<span class="input-group-addon">Employee ID</span>
							<input type="text" class="form-control" id="inputGroupSuccess1" aria-describedby="inputGroupSuccess1Status" placeholder="Your Employee Id">
						</div>
						<div class="input-group">
							<span class="input-group-addon">Password</span>
							<input type="password" placeholder="Your Password" class="form-control">
						</div>
						<div class="input-group">
							<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span> Log in</button>
						</div>
						<div class="input-group">
							<a href="#myModalLabel">
								<button type="button" class="btn btn-default" ><span class="glyphicon glyphicon-user" aria-hidden="true"></span>Sign up</button>
							</a>
						</div>
					</div>
				</form>
				{% endblock %}
			</div>
</body>
</html>




admin.py

"""
Django settings for mbrsgvp project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cc#6($5ix#vpzqj@#8$cl)pd4pia6v*_k-8!5&=5za9v6d2k2h'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mbrs',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

)

ROOT_URLCONF = 'mbrsgvp.urls'

WSGI_APPLICATION = 'mbrsgvp.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'student',
        'USER': 'gvpmca',
        'PASSWORD': '123456',
        'HOST': 'localhost',
        'PORT': '5432',
         }
}

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ os.path.join(BASE_DIR,'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

#STATIC_URL = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
                    os.path.join(BASE_DIR,'static'),
                     )
TEMPLATE_DIRS =(
    os.path.join(BASE_DIR,'templates'),
    ) 
Pasted now — Expires in 30 days
Reply all
Reply to author
Forward
0 new messages