My View :
def refer(request, seek_id):
seek_obj = SeekSolutions.objects.get(pk = seek_id)
if request.method == 'POST':
form = SeekReferForm(request.POST)
if form.is_valid():
obj = form.save(commit = False)
obj.refferer = request.user
obj.seek_id = seek_obj
obj.save()
send_message(obj)
return HttpResponseRedirect('/seek/referred')
return render_to_response('seek/refer.html', {'form' : form , } )
Traceback :
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
response = callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/options.py in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/views/decorators/cache.py in _wrapped_view_func
response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/sites.py in inner
return view(request, *args, **kwargs) ...
▶ Local vars
/Library/Fr开发者_StackOverflowameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapper
return bound_func(*args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in _wrapped_view
response = view_func(request, *args, **kwargs) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/decorators.py in bound_func
return func(self, *args2, **kwargs2) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/options.py in changelist_view
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py in __len__
self._result_cache = list(self.iterator()) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py in iterator
for row in compiler.results_iter(): ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py in results_iter
for rows in self.execute_sql(MULTI): ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql
cursor.execute(sql, params) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py in execute
return self.cursor.execute(sql, params) ...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute
return Database.Cursor.execute(self, query, params) ...
▶ Local vars
Although this question is pretty old, since I ran in to the same problem(and just solved it) I think I ought to answer to it for future references. But then I had a different error. But you seem to be missing something in your code.
def refer(request, seek_id):
seek_obj = SeekSolutions.objects.get(pk = seek_id)
if request.method == 'POST':
form = SeekReferForm(request.POST)
if form.is_valid():
obj = form.save(commit = False)
obj.refferer = request.user
obj.seek_id = seek_obj
obj.save()
send_message(obj)
return HttpResponseRedirect('/seek/referred')
else:
form = SeekReferForm()
return render_to_response('seek/refer.html', {'form' : form , } )
This includes an else
block to your first if
. This is because when you try to access the url from your browser, you are essentially doing a GET
request on the resource. As a result the first if is violated and comes directly to the return
statement where it tries to return a dictionary item with 'form'
as the key
and a form
object which was never created at all. Thus the error in your code. There need to be an else part which is supposed to instantiate the form
object in case you try to open the url in your browser. I hope that helps the cause.
the form is instantiated only when request.method == 'POST'.
精彩评论