請參閱下面的代碼。我試圖使用ModelForms將記錄添加到數據庫,但它不斷返回服務器錯誤 - 似乎與.save()操作相反,但我不太確定。任何幫助我如何解決這個問題將非常感激。ModelForm返回服務器錯誤(500)
#view.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, render_to_response, get_object_or_404
from django.template import RequestContext
from forms import ContactForm, wallForm
from django.core.mail import send_mail
from home.models import wall
from django.forms import ModelForm
def wallpost(request):
if request.method == 'POST':
new_post = wallForm(request.POST)
if form.is_valid():
f = form.cleaned_data['postContent']
new_post.save()
return HttpResponseRedirect('/home/')
else:
form = wallForm()
return render(request, 'home/wall_post.html', {'form': form,})
#model.py
from django.db import models
class wall(models.Model):
clusterId = models.ForeignKey(cluster)
userId = models.ForeignKey(user)
postContent = models.CharField(max_length=800)
likes = models.IntegerField(default=0)
post_timestamp = models.DateTimeField('date published')
clusterId.blank = True
userId.blank = True
postContent.blank = True
likes.blank = True
post_timestamp.blank = True
#forms.py
from django import forms
from django.forms import ModelForm
from home.models import wall
class wallForm(ModelForm):
class Meta:
model = wall
#template.py
<h1>Posting test</h1>
{% if form.errors %}
<p style="color: red;">
Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %}
<form action="" method="post">
<table>
{{ form.as_p }}
</table>
{% csrf_token %}
<input type="submit" value="Submit">
</form>
<b><a href = "/home/">Go back...</a></b>
謝謝,'new_post'最近才作爲我正在嘗試的一些調試的一部分被引入。我馬上試試你的代碼並重新發布。非常感謝! – user1953875
如果你在'settings.py'中設置了'DEBUG = True',你應該得到一個有用的棧跟蹤。 –