Re: Accessing Model field from ModelForm?

2008-11-01 Thread killsto

Thanks, that's a big help.

On Nov 1, 5:14 am, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Nov 1, 5:33 am, killsto <[EMAIL PROTECTED]> wrote:
>
>
>
> > I have a basic form that shows what a person entered when it is
> > submitted.  However, it shows up as a form.CharField (textbox) instead
> > of model.CharField. Is there a way to access the model's saved data
> > from the Forms? Also does form_for_model (http://www.djangobook.com/en/
> > 1.0/chapter07/) no longer exist? I can't get it to load.
>
> 
>
> > from qotd.models import *
> > from django.shortcuts import render_to_response
> > from django.http import HttpResponseRedirect
>
> > def contact(request, question):
> >     if request.method == 'POST': # If the form has been submitted...
> >         form = Questionform(request.POST) # A form bound to the POST
> > data
> >         if form.is_valid():
> >                 form.save()
> >                 #return HttpResponseRedirect('results.html', {'form': form}
>
> >     else:
> >         form = Questionform()
> >     return render_to_response('contact.html', {
> >         'form': form
> >     })
>
> The only thing you need to do that you're not doing is to actually
> pass the instance of Question (or Choice) to the form when you
> instantiate it.
>
> Assuming that 'question' is the PK of the Question object:
>
> def contact(request, question):
>     question_obj = Question.objects.get(pk=question)
>     if request.method == 'POST':
>         form = Questionform(request.POST, instance=question_obj)
>         if form.is_valid():
>                 form.save()
>                 return HttpResponseRedirect('/submission/')
>     else:
>         form = Questionform(instance=question_obj)
>     return render_to_response('contact.html', {
>         'form': form
>     })
>
> Note that HttpResponseRedirect takes a *URL*, not a template/context.
> It tells the user's browser to request a different URL so that they
> can't submit the form twice.
> --
> DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Accessing Model field from ModelForm?

2008-11-01 Thread Daniel Roseman

On Nov 1, 5:33 am, killsto <[EMAIL PROTECTED]> wrote:
> I have a basic form that shows what a person entered when it is
> submitted.  However, it shows up as a form.CharField (textbox) instead
> of model.CharField. Is there a way to access the model's saved data
> from the Forms? Also does form_for_model (http://www.djangobook.com/en/
> 1.0/chapter07/) no longer exist? I can't get it to load.
>

>
> from qotd.models import *
> from django.shortcuts import render_to_response
> from django.http import HttpResponseRedirect
>
> def contact(request, question):
>     if request.method == 'POST': # If the form has been submitted...
>         form = Questionform(request.POST) # A form bound to the POST
> data
>         if form.is_valid():
>                 form.save()
>                 #return HttpResponseRedirect('results.html', {'form': form}
>
>     else:
>         form = Questionform()
>     return render_to_response('contact.html', {
>         'form': form
>     })

The only thing you need to do that you're not doing is to actually
pass the instance of Question (or Choice) to the form when you
instantiate it.

Assuming that 'question' is the PK of the Question object:

def contact(request, question):
question_obj = Question.objects.get(pk=question)
if request.method == 'POST':
form = Questionform(request.POST, instance=question_obj)
if form.is_valid():
form.save()
return HttpResponseRedirect('/submission/')
else:
form = Questionform(instance=question_obj)
return render_to_response('contact.html', {
'form': form
})

Note that HttpResponseRedirect takes a *URL*, not a template/context.
It tells the user's browser to request a different URL so that they
can't submit the form twice.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Accessing Model field from ModelForm?

2008-11-01 Thread killsto

I have a basic form that shows what a person entered when it is
submitted.  However, it shows up as a form.CharField (textbox) instead
of model.CharField. Is there a way to access the model's saved data
from the Forms? Also does form_for_model (http://www.djangobook.com/en/
1.0/chapter07/) no longer exist? I can't get it to load.

models.py:
from django.db import models
from django.forms import ModelForm

class Question(models.Model):
question = models.CharField('Question', max_length = '200',
primary_key = True)
pub_date = models.DateField(auto_now_add = True)
def __unicode__(self):
return self.question

class Choice(models.Model):
Question = models.ForeignKey(Question)
choice = models.CharField(max_length = '200')

class Questionform(ModelForm):
class Meta:
model = Question
class Choiceform(ModelForm):
class Meta:
model = Choice

view:
from qotd.models import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

def contact(request, question):
if request.method == 'POST': # If the form has been submitted...
form = Questionform(request.POST) # A form bound to the POST
data
if form.is_valid():
form.save()
#return HttpResponseRedirect('results.html', {'form': form}

else:
form = Questionform()
return render_to_response('contact.html', {
'form': form
})
template:


{% if not form.is_valid %}

{{ form.as_p }}

{% endif %}
{% if form.is_valid %}
{{ form.question }}
{% endif %}




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---