0
我想在視圖中創建對象,但無法弄清楚爲什麼我會得到500
錯誤。無法在Django視圖中創建對象
下面是相關代碼:
lvt = LastVisitedTopic.objects.create(user=uid, topic=t.id, lastvisited=lv)
和模型是:
class LastVisitedTopic(models.Model):
user = models.ForeignKey(User)
topic = models.ForeignKey(Topic)
lastvisited = models.DateTimeField(auto_now=True)
class Meta:
managed = True
app_label = 'myforum'
傳遞給create方法的參數似乎是確定:
print 'uid, t.id, lv \n', uid, t.id, lv
產量:
uid, t.id, lv
1 202798 2014-10-19 03:10:00+00:00
我在這個股票一段時間,所以真的很感激你的線索。
UPDATE: 這裏是整個意見,在情況下,它可以幫助:
def notify_ajax(request):
#if request.method == 'GET':
args = {}
alerts = []
mt_alerts = []
uid = request.user.id
numalerts = 0
if Topic.objects.filter(Q(post__creator_id=uid) | Q(creator_id=uid)).exists():
mypartopics = Topic.objects.filter(Q(post__creator_id=uid) | Q(creator_id=uid)).distinct().order_by("-created")
for t in mypartopics:
print t.title
#Check whether there is record of the topic in LastVisitedTopic
if not LastVisitedTopic.objects.filter(topic_id=t.id, user_id=uid).exists():
print 'LastVisitedTopic not exists so to be created...'
#Calculate the record
#If topic has posts by this user
if Post.objects.filter(topic_id=t.id, creator_id=uid).exists():
print 'this topic has posts...\n'
#Last post time by this user is assumed as the last time he visited the topic
last_post = Post.objects.filter(topic_id=t.id, creator_id=uid).latest('created')
print 'latest post found'
lv = last_post.created
print 'lv to be added from post', lv
print 'uid, t.id, lv \n', uid, t.id, lv
lvt = LastVisitedTopic.objects.create(user=uid, topic=t.id, lastvisited=lv)
print 'last visit record created'
#Else last visit is assumed as the time when the topic is created
else:
lv = t.created
print 'lv added from topic'
lvt = LastVisitedTopic.objects.create(user=uid,topic=t.id, lastvisited='%s')
print 'last visit record created equal to topic creation time'
else:
print 'record for this topic exists',
track = LastVisitedTopic.objects.get(topic_id=t.id, user_id=uid)
print 'last visited at', track.lastvisited
#Check whether the topic lastposted field is not empty, if so, fill it
if not t.lastposted:
if Post.objects.filter(topic_id=t.id).exists():
topic_lp= Post.objects.filter(topic_id=t.id).latest('created')
t.lastposted = topic_lp.created
t.save()
print 'new t.lastposted saved'
else:
t.lastposted = t.created
t.save()
else:
print 't.lastposted for this topic exists', t.lastposted
#Now compare last visit by the user with last topic post
try:
print '\n\n\nnow trying...'
track = LastVisitedTopic.objects.get(topic_id=t.id, user_id=uid)
print 'track', track.id
last_visit = track.lastvisited
print 'last visit', last_visit
print 't.lastposted', t.lastposted
if t.lastposted > last_visit :
print 'topic posted after the last visit'
#last_posts = Post.objects.filter(topic_id=t.id).latest()
#if last_post.created >= last_visit:
alerts.append({'title':t.title, 'slug':t.slug})
else:
print 'lastposted before last visit'
except:
print 't excpeton'
pass
print 'alerts', alerts
print 'alerst length', len(alerts)
args['alerts'] = alerts
return render(request, '_alerts.html', args)
你正在得到什麼錯誤?你有沒有試過在dev服務器上做同樣的事情並進行調試? – arulmr
我只得到500:'[02/Mar/2016 14:43:56]「GET/notify HTTP/1.1」500 14683'。我在開發服務器上。並且請求是一個Ajax請求。 – Jand
你的設置中是否有'DEBUG = True'? – arulmr