Forums

Crispy Forms VariableDoesNotExist at / Failed lookup for key [form]

Hi,

I'm trying to create a simple crispy form by following this simple example:

https://gist.github.com/maraujop/1838193

My form.py:

class ContactForm(forms.Form):

fullname = forms.CharField(label ="Full Name",required=True)
email = forms.EmailField(label ="Your Email Address",required=True)
subject = forms.CharField(label ="Subject",required=True)
message = forms.CharField(widget=forms.Textarea)

My view.py

def formview(request):

return render(request, 'index.html', {'form': ContactForm()})

and in my template index.html:

{% load crispy_forms_tags %}

{% crispy form %}

Still I get the error:

VariableDoesNotExist at / Failed lookup for key [form] in "[{'False': False, ........

What am I missing? Please help

When I hit your web app to see the full error message, I can see that there is, indeed no form entry in the context, so my guess is that your index url is not actually pointing to formview.

Thanks for the hint.

It's still a mystery for me ..

If I understood you correctly the the 'index.html' in

def formview(request):
    return render(request, 'index.html', {'form': MessageForm()})

is not recognized.

A simple message print however works totally fine with the same URL

def index(request):
    # Request the context of the request.
    context = RequestContext(request)

    # Construct a dictionary to pass to the template engine as its context.
    context_dict = {'boldmessage': "Hello World"}

    # Return a rendered response
    return render_to_response('index.html', context_dict, context)

What else could be missing?

No, I meant that the URL in urls.py might not actually be pointing to formview, but to some other bit of code somewhere else. To check that, change the "index.html" in formview to something random, reload your web app and see whether you get the same error or a "template not found" error. If it's the same then you need to trace work out what code you are executing by looking at urls.py to determine what view you're actually calling.

OK I get the same error by changing index.html to random.html

I have to admit that I'm new to Django so I'm struggling with what's missing now in my urls.py

here is my actual urls.py

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

admin.autodiscover()
urlpatterns = patterns('',
     url(r'^$', 'project_vitamine.main_app.views.index', name='index'),
     url(r'^admin/', include(admin.site.urls)),

)

Thanks a lot for your help!

[edit by admin: formatting]

So, in there, you're saying that when the root URL of your site is hit, it will call the view function defined in project_vitamine.main_app.views.index -- not the one in project_vitamine.main_app.views.formview. You don't have any URLs wired up to the view called formview. So I think that's what the problem is.

Thanks for your comment. I understand your argument.

I tried

url(r'^$', 'project_vitamine.main_app.views.formview', name='form'),

or

url(r'^index/', 'project_vitamine.main_app.views.formview', name='form'),

but still having the same error.

Any other suggestions? I really appreciate your support.

I just hit /index/ on your site and got a 404, are you sure your urls.py is exactly as you show above?

this is my actual urls.py

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from project_vitamine.main_app.views import formview

from django.contrib import admin



admin.autodiscover()

urlpatterns = patterns('',

    url(r'^index', 'project_vitamine.main_app.views.index', name='index'),
    url(r'^index', 'project_vitamine.main_app.views.formview', name='form'),

    url(r'^reg/', include('project_vitamine.reg.urls')), 
    url(r'^admin/', include(admin.site.urls)),

)

I changed

url(r'^$', 'project_vitamine.main_app.views.index', name='index'),

to

url(r'^index', 'project_vitamine.main_app.views.index', name='index'),

and the 404 is gone.

I still have the crispy problem ... its just frustrating

I think the problem you're getting now is that you have two matches for the index URL. That is, the line:

url(r'^index', 'project_vitamine.main_app.views.index', name='index'),

...is telling Django, "when a user hits http://primeresults.pythonanywhere.com/index/, then run the view function called project_vitamine.main_app.views.index

Whereas the line

url(r'^index', 'project_vitamine.main_app.views.formview', name='form'),

...is telling Django, "when a user hits http://primeresults.pythonanywhere.com/index/, then run the view function called project_vitamine.main_app.views.formview

So you're giving Django two different sets of instructions on how to handle that URL. I think Django will just pick up the first one in that case.

Yeah ! It worked , thanks Giles !! Thanks a lot.

There is one thing left. If I hit

http://primeresults.pythonanywhere.com/

I get a 404. If I hit

http://primeresults.pythonanywhere.com/index/

everything works fine

Ho can I redirect

http://primeresults.pythonanywhere.com/

to

http://primeresults.pythonanywhere.com/index/ ?

Thanks

OK found the answer myself:

(r'^$', lambda r : HttpResponseRedirect('/index/')),

Thanks again for all your help!

Excellent, glad it's working!