I’m trying to post data from my django model form to my database, but am not having any luck. I can create from the admin and display on the page, but can’t seem to pass my information correctly. The comment is linked to the school with a foreign key, which I know works. Here’s what I have for my models, views, and html.
models.py
from django.forms import ModelForm
class Comment(models.Model):
school = models.ForeignKey(Schools)
created = models.DateTimeField(auto_now_add=True)
author = models.CharField(max_length=60)
body = models.TextField()
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['author', 'body', 'school']
views.py
EDITED
I also added the comment object create, just as another thing to try.
from my_app.models import Comment, CommentForm
if request.method == 'POST':
form = CommentForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
author = form.cleaned_data['author']
body = form.cleaned_data['body']
school = form.cleaned_data['school']
form.save()
content = Comment.objects.create(school = school, author = author, body = body)
I have tried many variations on this view, but have not had any luck yet.
html
<form action="/" method="POST">{% csrf_token %}
<p>{{ form.body }}</p>
<div id="submit"><input type="submit" value="Submit"></div>
</form>
A. You must specify either {{form.as_p}}
, {{form.as_table}}
or simply {{form}}
instead of {{form.body}}
. Check the docs for more details: https://docs.djangoproject.com/en/dev/topics/forms/
B. The cleaned_data
require an attribute from the Comment
class. So, change: body = form.cleaned_data['comment']
with body = form.cleaned_data['body']
C. Have fun with Django.
Edit
Move the if request.method == 'POST':
inside a new method, i.e. register_comment
:
def register_comment:
if request.method == 'POST':
form = CommentForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
author = form.cleaned_data['author']
body = form.cleaned_data['body']
school = form.cleaned_data['school']
form.save()
Modify the urls.py
:
urlpatters = patterns('',
# Some others views here
url(r'^new-comment/$', 'app_name.views.register_comment', name="new_comment"),
# Maybe Some others views here
)
Open your web browser, and go to: http://127.0.0.1:8000/new-comment/
I’m trying to post data from my django model form to my database, but am not having any luck. I can create from the admin and display on the page, but can’t seem to pass my information correctly. The comment is linked to the school with a foreign key, which I know works. Here’s what I have for my models, views, and html.
models.py
from django.forms import ModelForm
class Comment(models.Model):
school = models.ForeignKey(Schools)
created = models.DateTimeField(auto_now_add=True)
author = models.CharField(max_length=60)
body = models.TextField()
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ['author', 'body', 'school']
views.py
EDITED
I also added the comment object create, just as another thing to try.
from my_app.models import Comment, CommentForm
if request.method == 'POST':
form = CommentForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
author = form.cleaned_data['author']
body = form.cleaned_data['body']
school = form.cleaned_data['school']
form.save()
content = Comment.objects.create(school = school, author = author, body = body)
I have tried many variations on this view, but have not had any luck yet.
html
<form action="/" method="POST">{% csrf_token %}
<p>{{ form.body }}</p>
<div id="submit"><input type="submit" value="Submit"></div>
</form>
A. You must specify either {{form.as_p}}
, {{form.as_table}}
or simply {{form}}
instead of {{form.body}}
. Check the docs for more details: https://docs.djangoproject.com/en/dev/topics/forms/
B. The cleaned_data
require an attribute from the Comment
class. So, change: body = form.cleaned_data['comment']
with body = form.cleaned_data['body']
C. Have fun with Django.
Edit
Move the if request.method == 'POST':
inside a new method, i.e. register_comment
:
def register_comment:
if request.method == 'POST':
form = CommentForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
author = form.cleaned_data['author']
body = form.cleaned_data['body']
school = form.cleaned_data['school']
form.save()
Modify the urls.py
:
urlpatters = patterns('',
# Some others views here
url(r'^new-comment/$', 'app_name.views.register_comment', name="new_comment"),
# Maybe Some others views here
)
Open your web browser, and go to: http://127.0.0.1:8000/new-comment/
0 commentaires:
Enregistrer un commentaire