List Info

Thread: what is wrong with my New Forms code




what is wrong with my New Forms code
country flaguser name
United States
2007-09-27 22:52:36
I tried the following but it fails in flames with 
"http response 
required..."   I've followed the documentation - am I
called the 
function correctly by a url with
"/mymodel_addedit"    Can someone help ?

urls.py
r'^mymodel/addedit/$',
'myproject.myapp.views.mymodel_addedit'),

views.py
def mymodel_addedit(request, id=None):
if id is None:
  MyModelForm = forms.models.form_for_model(MyModel)
else:
  mymodel = MyModel.objects.get(id=id)
  MyModelForm = forms.models.form_for_instance(mymodel)
  if request.method == 'POST':
    form = MyModelForm(request.POST)
    if form.is_valid():
      mymodel = form.save(commit=False)
      mymodel.save()
      return HttpResponseRedirect("/")
  else:
    form = MyModelForm()
  return render_to_response('mymodel_addedit.html', {'form':
form})

/templates/mymodel_addedit.html
{% extends "base.html" %}

{% block content %}
<form action="." method="post">
  <table class="form">
      {}
  </table>
  <input type="submit" value="Submit"
/>
</form>
{% endblock %}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: what is wrong with my New Forms code
user name
2007-09-27 23:03:16
It is hard to see with the indentation as it is, but it
seems you are  
not returning an http response if the 'if id is none:'  You
are  
building the form for model, but it doesnt look like it is
returned.   
The bottom line seems to be indented to correlate to the if 

request.method not the first if.


-richard

On Sep 27, 2007, at 10:52 PM, staff-gmail wrote:

>
> I tried the following but it fails in flames with 
"http response
> required..."   I've followed the documentation -
am I called the
> function correctly by a url with
"/mymodel_addedit"    Can someone  
> help ?
>
> urls.py
> r'^mymodel/addedit/$',
'myproject.myapp.views.mymodel_addedit'),
>
> views.py
> def mymodel_addedit(request, id=None):
> if id is None:
>   MyModelForm = forms.models.form_for_model(MyModel)
> else:
>   mymodel = MyModel.objects.get(id=id)
>   MyModelForm =
forms.models.form_for_instance(mymodel)
>   if request.method == 'POST':
>     form = MyModelForm(request.POST)
>     if form.is_valid():
>       mymodel = form.save(commit=False)
>       mymodel.save()
>       return HttpResponseRedirect("/")
>   else:
>     form = MyModelForm()
>   return render_to_response('mymodel_addedit.html',
{'form': form})
>
> /templates/mymodel_addedit.html
> {% extends "base.html" %}
>
> {% block content %}
> <form action="."
method="post">
>   <table class="form">
>       {}
>   </table>
>   <input type="submit"
value="Submit" />
> </form>
> {% endblock %}
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: what is wrong with my New Forms code
country flaguser name
United States
2007-09-28 10:20:02
Richard Dahl wrote:
> It is hard to see with the indentation as it is, but it
seems you are  
> not returning an http response if the 'if id is none:' 
You are  
> building the form for model, but it doesnt look like it
is returned.   
> The bottom line seems to be indented to correlate to
the if  
> request.method not the first if.
>   
thks - you are exactly right - I fixed the indentation and
all is well. 


>
> -richard
>
> On Sep 27, 2007, at 10:52 PM, staff-gmail wrote:
>
>   
>> I tried the following but it fails in flames with 
"http response
>> required..."   I've followed the documentation
- am I called the
>> function correctly by a url with
"/mymodel_addedit"    Can someone  
>> help ?
>>
>> urls.py
>> r'^mymodel/addedit/$',
'myproject.myapp.views.mymodel_addedit'),
>>
>> views.py
>> def mymodel_addedit(request, id=None):
>> if id is None:
>>   MyModelForm =
forms.models.form_for_model(MyModel)
>> else:
>>   mymodel = MyModel.objects.get(id=id)
>>   MyModelForm =
forms.models.form_for_instance(mymodel)
>>   if request.method == 'POST':
>>     form = MyModelForm(request.POST)
>>     if form.is_valid():
>>       mymodel = form.save(commit=False)
>>       mymodel.save()
>>       return HttpResponseRedirect("/")
>>   else:
>>     form = MyModelForm()
>>   return render_to_response('mymodel_addedit.html',
{'form': form})
>>
>> /templates/mymodel_addedit.html
>> {% extends "base.html" %}
>>
>> {% block content %}
>> <form action="."
method="post">
>>   <table class="form">
>>       {}
>>   </table>
>>   <input type="submit"
value="Submit" />
>> </form>
>> {% endblock %}
>>
>>
>>     
>
>
> >
>
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )