Thursday, February 2, 2012

Re: Entering Text into a Database

Hey dude,

Let's say you have some model with fields defined. It's called Business. Looks something like this:

class Business(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField("Business Name", max_length=NAME, blank=False)

You create a ModelForm like this:

class BusinessDetailsForm(forms.ModelForm):
    class Meta:
        model = Business

In your view you have a method that captures the POST and saves it inside the model:

def save_business_details(request):
    if request.POST:
        form = BusinessDetailsForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse("your_app.views.some_view_when_submission_is_successful", args=[]))
    else:
        here you pass back the form or whatever
    return render_to_response(and render that whatever)

So, form.save() will save the details.

I hope that helps!

-m

On 3 February 2012 14:40, Python_Junkie <software.buy.design@gmail.com> wrote:
Not sure if this will help, but I have diverted from the standard
method of database updates taught in the tutorial.

I take a more traditional sql methodology

for example in any view that I am using.

I collect the data elements via a request
and then build a sql statement

for example

(The exact syntax may be a little off.)

var_1=request.post(name)

var_2=...etc

insert into table 1(col_1,col_2) values ('var_1,var_2)

commit


Let me know if this helps and I can update the syntax to be more
precise.

On Feb 2, 9:51 pm, ajohnston <ajohnston...@gmail.com> wrote:
> On Feb 2, 2:30 pm, ds39 <sdavid...@gmail.com> wrote:
> Is there any page, outside of the
>
> > official documentation, that gives an example from beginning to end
> > regarding how to save ModelForms in your database using views.py
> > rather than the shell (including sample URLs) ? Also, how would I
> > access the entered text via the shell to determine if it was saved ?
>
> Did you do the tutorial[1]?. Be sure to do all four parts. After that
> the examples in the ModelForms documentation[2] should make sense. If
> not, ask specific questions about what is not working the way you
> think it should. Good luck.
>
> {1]https://docs.djangoproject.com/en/dev/intro/tutorial01/
> [2]https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment