Sunday, September 7, 2014

Re: Save data from form

Thank you a lot for reply. I thought I could cover Django on fly, but it seems like I have to read some books before. Anyway thank you very much!

воскресенье, 7 сентября 2014 г., 10:50:55 UTC+3 пользователь Eddilbert Macharia написал:
Hi Aptem,

Your questions is not clearly what exactly do you want, where are you stuck, specifically,
Check this it might assist you https://docs.djangoproject.com/en/1.7/topics/forms/

this is a very basic example if you have an organization mode below

models.py
class Organization(models.Model):
name = models.CharField(max_length=25)

create a forms module and add this
form.py
class NameForm(form.Form):
name = forms.CharField(max_length=25)
 
 view.py
import the model and form to you view
def organization_save(request):      # if this is a POST request we need to process the form data      if request.method == 'POST':          # create a form instance and populate it with data from the request:          form = NameForm(request.POST)          # check whether it's valid:          if form.is_valid():              # process the data in form.cleaned_data as required
cleaned_name = form.cleaned_data['name']
            # save form if its new organization creation
 organization = Organization.objects.create(name=cleaned_name)
        # if its an existing organizanization
organization = Organization.objects.get(id='the id of the organization')
organization .name = cleaned_name
organization .save()
            # redirect to a new URL:              return HttpResponseRedirect('/thanks/')        # if a GET (or any other method) we'll create a blank form      else:          form = NameForm()        return render(request, 'name.html', {'form': form})


there is an easier way of doing this by use of modelforms check this out  https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/

Hope it assists you.

Regards,
Eddilbert Macharia.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61b0994b-5d25-4ef1-bac7-23475e33f745%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment