0

I just started to learn Django, I have a page width a form to input data from users and save it to database. i have searched on Google but most of there use some sort of built-in modules or something like this.

I have created a model

class Message(models.Model):
    MessageID = models.AutoField(verbose_name='Message ID',primary_key=True)
    MessageSubject = models.CharField(verbose_name='Subject',max_length=255)
    MessageContent = models.TextField(verbose_name='Content',)

and the view page.

{% extends "base.html" %}

{% block Content %}

<div class="container">
  <!-- Contacts -->
  <div id="contacts">
    <div class="row">
      <!-- Alignment -->
      <div class="col-sm-offset-3 col-sm-4">
        <!-- Form itself -->
        <form name="sentMessage" class="well" id="contactForm"  novalidate>
          <legend>Send Messages</legend>
          <div class="control-group">
            <div class="controls">
              <input type="text" class="form-control"
              placeholder="Subject" id="name" required
              data-validation-required-message="Please enter subject" />
              <p class="help-block"></p>
            </div>
          </div>


          <div class="control-group">
            <div class="controls">
              <textarea rows="10" cols="100" class="form-control"
              placeholder="Message" id="message" required
              data-validation-required-message="Please enter your message" minlength="5"
              data-validation-minlength-message="Min 5 characters"
              maxlength="999" style="resize:none"></textarea>
            </div>
          </div>
          <div class="radio">
            <label>
              <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
              Send Sms to Selected Users
            </label>
          </div>
          <div class="radio">
            <label>
              <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
              Send Email To Selected Users
            </label>
          </div>
          <div id="success"> </div>
          <button type="submit" class="btn btn-primary btn-sm pull-right">Send</button><br />
        </form>
      </div>
    </div>
  </div>
</div>



{% endblock %}

I want when user input the data into form and click Send button the data will store in the database table. I am using sqlite database.

3 Answers 3

2

Have you read the 6 parts of first steps in Django documentation already? If you do so, you may find something similar in "Writing your first Django app, part 4". The vote function in views.py just read the input data from users and save it to database.

By the way, I find something wrong in your code already.

<form name="sentMessage" class="well" id="contactForm"  novalidate>

You didn't provide a action attr for <form> tag, so your data in the form had no place to submit.

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

Comments

0

How do you create your view.page? Your view.py, [forms.py]??

for example, 1) My model CreateAccount https://github.com/evgenyivanov/form/blob/master/testingform/models.py

in template:

<form action="/test/" method="post" id="form">
{{form.as_p}}
 <br />
 <input id="submit" type="hidden" value="Send">
 </form>

in url.py

   url(r'^test/', 'testingform.views.test', name='test')

in view.py

   def test(request):
      if request.method == 'POST':
          form = CreateAccountForm(request.POST)
          if form.is_valid():
              return HttpResponse('OK')
      else:
            return HttpResponse(json.dumps(form.errors))

in form.py

  class CreateAccountForm(ModelForm):
    class Meta:
    model = CreateAccount

For more information about working with forms: http://www.pythoncentral.io/using-python-django-modelform-first-django-application/

Comments

0

You should not create an HTML form yourself, let Django do it.
First create a forms.py file in your app:

class MessageForm(forms.ModelForm):
    class Meta:
        model = Message
        fields = "__all__"

Then your template could be something like this:

{% extends "base.html" %}
{% load staticfiles %}

{% block content %}

<form method="POST">
    {% csrf_token %}
    {{ form }}
    <button type="submit" value="submit">Submit</button>
</form>

{% endblock content %}

And the view:

def message(request, message_id):
    message = get_object_or_404(Message, pk=int(message_id))
    if request.method == 'POST':
        form = MessageForm(request.POST, instance=message)
        if form.is_valid():
            form.save()
            return redirect("message_list")
    else:
        form = MessageForm(instance=message)
    context = {
        "form": form,
    }
    return render(request, 'message.html', context)

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.