5

So I already have a database setup with a few columns and a few rows already inserted in. I'm trying to create a view that you would just input information into a form and press Submit, then a row would be added to the MySQL database with the information you just typed in.

I believe you can do this with admin, but I would like to try without admin and I'm not sure if this is possible? I've been using the MySQL commandline to add rows as of now..

3 Answers 3

13

Of coures this is possible this is a building block for data driven websites. You can use a ModelForm as Daniel suggested (they offer built in validation and HTML markup for FREE) to easily map your model to a front end form. It would probably be beneficial to start with django tutorial or documentation first.

At the the very basic, all you have to do is instantiate your model

new_entry = YourModel(name='me', age='222', about='stackoverflow')

then save it

new_entry.save()

This adds it as a new row to your db.

https://docs.djangoproject.com/en/dev/topics/db/models/

Sign up to request clarification or add additional context in comments.

Comments

1

Why would it not be possible?

You probably want a modelform (but see the general form introduction first).

Comments

0

Try out this example of Generic Views: http://postneo.com/2005/08/17/django-generic-views-crud (assumes a model named Task)

With Generic Views you get Insert, Update and Delete for free without any real work. give it a try and let me know what you think.

from django.conf.urls.defaults import *

info_dict = {
    'app_label': 'tasks',
    'module_name': 'tasks',
}

urlpatterns = patterns('',
    (r'^tasks/create/?$', 'django.views.generic.create_update.create_object', info_dict ),
    (r'^tasks/update/(?P<object_id>\d+)/?$', 'django.views.generic.create_update.update_object', info_dict),
    (r'^tasks/delete/(?P<object_id>\d+)/?$', 'django.views.generic.create_update.delete_object', info_dict ),
)

Django Docs: https://docs.djangoproject.com/en/1.2/ref/generic-views/#create-update-delete-generic-views

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.