csrf question

46 views
Skip to first unread message

G Z

unread,
May 15, 2014, 2:35:46 AM5/15/14
to django...@googlegroups.com
So I read the documentation on passing csrf tokens, however its giving me an issue i think its because im trying to pass it as a dictonary variable with my form and customers.

This is from the documentation

from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render

@csrf_protect
def my_view(request):
    c = {}
    # ...
    return render(request, "a_template.html", c)


from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms
from .forms import SignUpForm
from vmware.models import Vmspecs
from django.views.decorators.csrf import csrf_protect

@csrf_protect
def index(request):
        c = {}
        form = SignUpForm(request.POST or None)
        if form.is_valid():
                save_it = form.save(commit=False)
                save_it.save()
        customers = Customer.objects.all()
        ctx = { 'customers':customers, 'form':form, c}
        return render_to_response('index.html', ctx)



this is the error i get 


Environment:


Request Method: GET

Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'vmware')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  101.                 resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  337.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  365.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  360.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/root/djangoprojects/provisioning/provisioning/urls.py" in <module>
  7. url(r'^customers/', include('vmware.urls')),
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/root/djangoprojects/provisioning/vmware/urls.py" in <module>
  5. from vmware import views

Exception Type: SyntaxError at /
Exception Value: invalid syntax (views.py, line 19)


Bob Gailer

unread,
May 15, 2014, 7:06:52 AM5/15/14
to django...@googlegroups.com

Line 19 (above) syntax error at /, c/

> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d9c8f4e7-1636-45d4-9877-2b01bdaf357b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

donarb

unread,
May 15, 2014, 10:12:33 AM5/15/14
to django...@googlegroups.com
Specifically, you're passing 'c' into the ctx dictionary, but it has no key, that's the syntax error. But that c dictionary is redundant anyway since you've created your own context dictionary. Just remove the declaration of that object and don't pass it into the dictionary. 

G Z

unread,
May 15, 2014, 11:42:30 AM5/15/14
to django...@googlegroups.com
What do you mean,
If i take it out of the ctx dictionary how do I pass it. if I just declare the @csrf_protect I still get the token error trying to post my form, infact the csrf issue is the only thing I have left to fix. How are you supposed to do the csrf token because im following the documentation and its not working. I tried what you suggested removing the declaration no luck.

G Z

unread,
May 15, 2014, 11:49:35 AM5/15/14
to django...@googlegroups.com
solved it


from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms
from .forms import SignUpForm
from vmware.models import Vmspecs
from django.views.decorators.csrf import csrf_protect
from django.template import Template, RequestContext

def index(request):
        form = SignUpForm(request.POST or None)
        if form.is_valid():
                save_it = form.save(commit=False)
                save_it.save()
        customers = Customer.objects.all()
        ctx = { 'customers':customers, 'form':form}
        return render_to_response('index.html', ctx, context_instance=RequestContext(request))
Reply all
Reply to author
Forward
0 new messages