title: Web text input save into database without form- Django Javascript
body: <p>I have an sqlite3 database setup with django 1.6 where I want web users to be able to enter text and see it in 3 live tables (high, low and latest) all on the same page. The one page ideally should have text entry, voting, display of the three tables updated when new entries go in or are voted on, and a search if possible if all that gets sorted (personal project, not commercial). I am also on win7 64 if that matters...</p>
  
  <p>Currently I have: a working database the three tables displaying in a web page without update and some web text input via js (and failing to save to the database in Django).</p>
  
  <p>I shied away from forms at first, as they seem to want separate html pages for input. Asking an experienced django coder, he helped me out with some javascript for text entry on the page. He said I didn't need to do it via forms and POST, GET, as the text was just going in with the initial score of 0 and the current datetime.</p>
  
  <p>My issue now is that I cannot get the entered text to save into the database without error.</p>
  
  <p>Since I need to program this in 2-3 weeks and am new to django (and oblivious to javascript, though I've done some Processing with PHP), my questions are;</p>
  
  <ul>
  <li>Am I missing something obvious with the text input save to database?</li>
  </ul>
  
  <p>-and-</p>
  
  <ul>
  <li>Is there a way to have all this using forms and GET, POST in one page
  so I can avoid a lot of javascript (unless it is truly easier)?</li>
  </ul>
  
  <p>I am going to start to try to build this with Forms at the moment, but hope for a bit of guidance on best practice from wiser heads.</p>
  
  <p>Here's the code so far:</p>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>from django.conf.urls import patterns, include, url
  from django.contrib import admin
  admin.autodiscover()
  
  urlpatterns = patterns('',
      url(r'^admin/', include(admin.site.urls)),
      url(r'^i/$', 'entries.views.index'),
      url(r'^add/(.*)$', 'entries.views.add'),
  )
  </code></pre>
  
  <p><strong>Models.py</strong></p>
  
  <pre><code>from django.db import models
  import datetime
  from django.utils import timezone
  
  class Entry(models.Model):
      text = models.CharField(max_length=15)
      score = models.IntegerField(default=0)
      pub_date = models.DateTimeField('date published')
      def __unicode__(self):
          return self.text
      def was_published_recently(self):
          return self.pub_date &gt;= timezone.now() - datetime.timedelta(days=1)
      was_published_recently.admin_order_field = 'pub_date'
      was_published_recently.boolean = True
      was_published_recently.short_description = 'Published recently?'
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code> &lt;html&gt;
   &lt;head&gt;
   &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"&gt;&lt;/script&gt;
   &lt;/head&gt;
   &lt;body&gt;
   &lt;ul&gt;
   {% for entry in latest_entry_list %}
      &lt;li&gt;&lt;a href="/entries/{{ entry.id }}/"&gt;{{ entry.text }}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp{{ entry.score }}&lt;/a&gt;&lt;/li&gt;
   {% endfor %}
   &lt;/ul&gt;
   &lt;ul&gt;
   {% for entry in high_entry_list %}
      &lt;li&gt;&lt;a href="/entries/{{ entry.id }}/"&gt;{{ entry.text }}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp{{ entry.score }}&lt;/a&gt;&lt;/li&gt;
   {% endfor %}
   &lt;/ul&gt;
   &lt;ul&gt;
   {% for entry in low_entry_list %}
      &lt;li&gt;&lt;a href="/entries/{{ entry.id }}/"&gt;{{ entry.text }}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp{{ entry.score }}&lt;/a&gt;&lt;/li&gt;
   {% endfor %}
   &lt;/ul&gt;
  &lt;style type="text/css" media="screen"&gt;
    div h2 span { color: #ff0000; }
    div span { color: #00ff00; }
    #box { width: 400px; height: 400px; }
    #h { color: #ff0000; }
  &lt;/style&gt;
  &lt;h3 id="h"&gt;title&lt;/h3&gt;
  
  &lt;p&gt;message: {{ text }}&lt;/p&gt;  
  &lt;input type="text" name="word" value="" id="input"/&gt;
  
  &lt;script type="text/javascript" src="{{STATIC_URL}}post.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>post.js</strong></p>
  
  <pre><code>console.log("hi from js");
  
  $(document).ready(function() {
  
  $("#input").bind("keypress", function(e) {
  
      //enter key pressed
      if (e.keyCode == 13) {
  
          var args = {};
  
          var text = $("#input").val();
  
          $.get("/add/" + text, args).done(function(data) {
              console.log("message: " + data);
          });
      }
  
  });
  
  });
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.shortcuts import render
  from django.http import HttpResponse
  from entries.models import Entry
  
  from django.db import models
  import datetime
  from django.utils import timezone
  
  def index(request):
      context = {
        'latest_entry_list': Entry.objects.order_by('-pub_date')[:10],
        'high_entry_list': Entry.objects.order_by('-score')[:10],
        'low_entry_list': Entry.objects.order_by('score')[:10],
      }
      return render(request, 'entries/index.html', context);
  
  def add(request, thingtoadd):
      #created_date = models.DateTimeField('date published', default=datetime.now)
      #created_score = '0'
      #created_text = 'test'   
      #e = Entry(text=created_text, score=created_score,pub_date=created_date)
      #e.save()
  
      return HttpResponse('done')
  </code></pre>
  
  <p>I am unsure of defining the fields for populating the Entry....does the above look right?</p>
  
  <p>I can uncomment the e=Entry(etc...) without error,</p>
  
  <p><strong>but when I uncomment the e.save(), the error is:</strong></p>
  
  <pre><code>GET http://127.0.0.1:8000/add/a 500 (INTERNAL SERVER ERROR) jquery.min.js:4
  send jquery.min.js:4
  n.extend.ajax jquery.min.js:4
  n.(anonymous function) jquery.min.js:4
  (anonymous function) post.js:15
  n.event.dispatch jquery.min.js:3
  r.handle
  </code></pre>
  
  <p>I will be getting on with trying to do this in forms, but wonder if there is some good advice as to if that is possible - I would ideally like to avoid js extras as I am very unfamiliar with it and it would be another level of unknowns at this point. Any input greatly appreciated...</p>