1

I apologize for asking a newbie question. I know both python and sqlite, but I never worked with them for a website with django framework.

So for example, my database already has 2 fields A and B. I want to insert dataA and dataB to A and B respectively. I want to take the input from 2 textbox on my website and put these data into the database as an entry.

How can I do that? If this is not too easy, where can I find in the documentation about this information?

1
  • 2
    In the tutorial, naturally. Part 4 talks about forms, but looks like you need to do the whole thing. Commented Jun 6, 2015 at 17:56

1 Answer 1

1

Sounds like you can use ModelForm for this purpose.

Just create your models like so:

class YourModel(models.Model):
    textA = models.CharField(max_length=200,)
    textB = models.CharField(max_length=200,)
    ...

Then, create your form:

class YourModelForm(forms.ModelForm):
    class Meta:
        model = YourModel
        fields = ['textA', 'textB']
        ...

Then, reference this from your view:

class YourCreateView(CreateView):
    form_class  = YourModelForm

Render this in your template ({{ form }}}) and you're done (mostly)!

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

2 Comments

Where is the part I update this entry in the database?
It depends on how you'd like to do that. For example, if you need this to be done by users, you might change the CreateView to UpdateView. Or, you might use the Django Admin for staff updates. Or, just change the data via the shell using the Django ORM.

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.