Django Forms: A Beginners Guide
I have been struggling to create a django form which users can fill and the filled data gets saved in the database. I used the docs available for django modelforms but was unsuccessfull in building a working form. I even googled for howto's about building a form that saves data to the database, there are hardly any available. When I tried to learn I ended up asking questions here (on this site). But however many 开发者_开发技巧questions I ask, I should be specific about my issue in the questions I ask, which means if one issue is solved (from the answers here) another issue is going to crop up. So my request is it would be a great help if some one could come up with a basic form using formsets with a relevant model (with a charfield, choicefield, radiofield, textarea) view, template (form and redirect), urls. And also a way to view the user-entered data in the admin page. At least whovever wants to build a django-form he/she can have something to begin with, which can be modified or improved upon without much hassel.
I started writing up a long post about this based on a site I created... but the issue is that I was just (poorly) reproducing this: http://docs.djangoproject.com/en/1.2/intro/tutorial01/
My advice to you is to go through this tutorial. I personally found it extremely helpful when I was picking up Django. In part four there's a great example of submitting user data from a template, dealing with it in a view, and saving it using a model.
Edit: based on your comment below I see you're using modelform. I've used this once before, but found it a bit limited when it came to styling. When I did, I followed this walkthrough: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
Note that you still have to include the form data in your template, as well as generate the form in your view, although the modelform does take some of the work out of it for you. Here's another walkthrough you may find useful: http://themorgue.org/blog/2008/05/14/django-and-modelform/
精彩评论