How do I get POST data from a free text field?
Question: Doing a modified version of the polls tutorial. Comments work with the
database when I go in the python manage.py shell but I can't get it to
actually read the post data. Any time I post a comment, the page re-renders
but no comment in the database.

Here are my models for an individual Entry and a Comment

    
    
    import datetime
    from django.db import models
    from django.utils import timezone
    from django.forms import ModelForm
    
    class Entry(models.Model):
        title = models.CharField(max_length=200)
        body = models.TextField()
        pub_date = models.DateTimeField('date published')
    
    class Comment(models.Model):
        entry = models.ForeignKey(Entry)
        comment = models.TextField()
        comment_date = models.DateTimeField()
    

In the Python shell, I'm able to create comments (that show up in the admin)
perfectly.

    
    
    >>> from blog.models import Entry, Comment
    >>> e = Entry.objects.get(pk=1)
    >>> from django.utils import timezone
    >>> e.comment_set.create(comment="isn't it pretty to think so?", comment_date=timezone.now())
    <Comment: isn't it pretty to think so?>
    

In the detail.html view of each blog entry, a user can add a comment.

    
    
    <h1>{{ entry.title }}</h1>
    <p>{{ entry.body }}</p>
    <p>{{ entry.tags_set.all }}</p>
    
    
    <form action="{% url 'blog:comment' entry.id %}" method="post">
    {% csrf_token %}
    <textarea name="comment101" style="width:300px; height: 70px; maxlength="300"; display:none;">
    </textarea></br>
    <input type="submit" name="comment101" value="Add comment" />
    </form>
    

Views for detail and comment:

    
    
    from django.utils import timezone
    from django.shortcuts import render, get_object_or_404
    from django.http import HttpResponse, HttpResponseRedirect
    from django.core.urlresolvers import reverse
    
    from blog.models import Entry, Tags, Comment
    
    def detail(request, entry_id):
        entry = get_object_or_404(Entry, pk=entry_id)
        return render(request, 'entries/detail.html', {'entry': entry})
    
    def comment(request, entry_id):
        p = get_object_or_404(Entry, pk=entry_id)
        add_comment = request.POST['comment101']
        #get input name comment from POST data
        p.comment_set.create(comment="add_comment", comment_date=timezone.now())
        return HttpResponseRedirect(reverse('blog:detail', args=(p.id)))
    

I've exhausted all I know. I tried adding name='comment101' every input/form
in detail.html and my comment view replicates exactly what I did in the Python
shell.

Lastly, if anyone could point me to something to debug code involving POST
data (for Mac), that'd be helpful. Thank you.


Answer: I would use

    
    
    c = Comment(comment=add_comment, comment_date=timezone.now(), entry=p)
    c.save()
    

That might be of some use.

Also if you want to debug code it is best to place

    
    
    import pdb; pdb.set_trace()
    

which gives you interactive console wherever you want it.

In case you want to use something better use ipdb (pip install ipdb)

    
    
    import ipdb; ipdb.set_trace()
    

