title: A Very, Very Basic JavaScript Button in Django
body: <p>I know a bunch of Django, HTML, and CSS, but I somehow never got around to do anything in  JavaScript (only did a little jQuery).</p>
  
  <p>I want to use this on a simple website for the time being for pressed buttons whose look and relevant database changes without reloading the page. I would like a simple example using Django and maybe some jQuery to start learning it.</p>
  
  <p>Let’s just use a Favorite/Like button known from, say, Twitter as an example.</p>
  
  <p>The button has to</p>
  
  <ul>
  <li>Let a user
  <ul>
  <li>favorite a post</li>
  <li>save the choice (i.e. store it in the related MySQL DB)</li>
  </ul></li>
  <li>Change the text and look of the button without loading a new page</li>
  </ul>
  
  <p>How would I go about this?</p>
  
  <p>Here is the boilerplate code to start it off:</p>
  
  <h2>Django</h2>
  
  <pre><code>### models.py
  from django.db import models
  from django.contrib.auth.models import User
  
  class Post(models.Model):
      likes = ManyToManyField(User, null=True, blank=True, related_name="likes")
  
  ### views.py
  def post(request, post_id):
      if request.method != 'POST':
          render(request, 'mytemplate.html',
                  {'post': get_object_or_404(Post, pk=post_id)})
      else:
          # ...?
  </code></pre>
  
  <h2>HTML Template</h2>
  
  <pre><code>&lt;a class="favorite" href="#" title="Like this post"&gt;Like?&lt;a&gt;
  </code></pre>