Tuesday, October 7, 2014

Re: How do I create a simple user view web page to create/add mysql db entries?

Using a plan ModelForm in a few to create objects is surprisingly not well documented :)
https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/

Basically you do something like this:

from django import forms

class MyForm(forms.ModelForm):
   
class Meta:
        model
= MyModel
        fields
= ['field1', 'field2']

@login_required  # if you need it
def add_view(request):
    form
= MyForm()
   
if request.method == 'POST':
        form
= MyForm(request.POST)
       
if form.is_valid():
            the_new_entry
= form.save()
           
return redirect()  # wherever you want to go next
   
return render(request, 'add_template', {'form': form})


--
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/61fb9b6d-fe6c-4501-b562-b8805efd838b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment