fark işleyen görünüm için csrf_exempt dekoratör kullanın. Bu, önce bir GET isteği gerçekleştirilerek ve ardından uygun bir ayrıştırıcı kullanılarak csrfmiddlewaretoken
ayrıştırılarak gerçekleştirilebilir.
Ayrıca, bunun işe yaraması için bir kurabiye kabı yüklemeniz gerekeceğini unutmayın. Örneğin
: oturumunuz geçersiz, çünkü tekrar giriş yapın
#!/usr/bin/python
import urllib, urllib2, cookielib
from BeautifulSoup import BeautifulSoup
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
url = urllib2.urlopen('http://localhost:8000/accounts/login/')
html = url.read()
doc = BeautifulSoup(html)
csrf_input = doc.find(attrs = dict(name = 'csrfmiddlewaretoken'))
csrf_token = csrf_input['value']
params = urllib.urlencode(dict(username = 'foo', password='top_secret',
csrfmiddlewaretoken = csrf_token))
url = urllib2.urlopen('http://localhost:8000/accounts/login/', params)
print url.read()
yukarıda kodunuzu çalıştım ama (html) aşağıdaki hatayı alıyorum. Oturumu canlı tutmayı biliyor musun? (Localhost ile denedim: 8000/admin) – abrunet
Ekle opener.addheaders = [('Referer', login_url)] '. En azından bazı Django dağıtımları, [bu yanıt] 'a göre bir _Referer_ başlığı gerektirecektir (http://stackoverflow.com/questions/5082128/how-do-i-authenticate-a-urllib2-script-in-order-to-access -HTTPS-web hizmetleri-fro) – nmgeek