LINUX.ORG.RU

django 1.4 post запросы

 


0

1

Добрый вечер! Осваиваю джангу, но наткнулся на проблему, 2 дня проб и ошибок результатов не дали.
Самый простой post запрос: на странице форма, в ней 2 поля - логин, пароль, и кнопка отправки данных.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Np</title>
</head>
<body>
	<form action="/login/" method="post">{% csrf_token %}
		<input type="text" name="username"><br/>
		<input type ="password" name="password"> 
		<input type="submit" value="login">
	</form>
</body>
</html>

Функция обработчик
from django.shortcuts import render_to_response
from django.template import RequestContext

def login(request):
    username = request.POST["username"]
    password = request.POST["password"]
    return render_to_response("index2.html", {'username': username, "password": password, },context_instance=RequestContext(request),)

index2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Np</title>
</head>
<body>
	{{ username }}
	{{ password }}
</body>
</html>

после запроса имею такой текст
CSRF verification failed. Request aborted.

Help
Reason given for failure:
    CSRF cookie not set.
    
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 uses RequestContext for the template, instead of Context.
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.


в настройках CsrfViewMiddleware есть, вроде делал все по https://docs.djangoproject.com/en/1.4/intro/tutorial04/, с get запросом все нормально, все корректно отображается, система ubuntu 12.04, eclipse.
скорей всего ответ где то на поверхности, но я его не вижу, прошу вашей помощи



Последнее исправление: matroskin (всего исправлений: 1)

Да все нормально. Твой код работает. Если у тебя не так, то показывай весь код.

gruy ★★★★★
()
Ответ на: комментарий от gruy

стоило задать вопрос, как сам и разобрался. как оказалось, RequestContext необходимо было в функции которая формирует страницу, а не обрабатывает. мне стыдно, я невнимательно смотрел вывод отладчика.
gruy - спасибо что посмотрели этот код

matroskin
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.