weixin_33726313 2011-10-06 09:31 采纳率: 0%
浏览 53

Django-Ajax隐藏模型表格

I have these 2 models:

class Exercise(models.Model):
  text = models.TextField()

class Score(models.Model):
  user = models.ForeignKey(User)
  exercise = models.ForeignKey(Exercise)
  score = models.IntegerField()
  class Meta:
    unique_together = (('user', 'exercise',),)

A template displays an exercise that a user can complete. The user gets a score at the end.

I'd like to send the score to the database with ajax.

I prepared a view to get the scores

def scoresexo(request):
  if request.method == 'POST':
    ret = {'type': 'info'}
    score, created = Score.objects.get_or_create(
      user = request.user
    , exercise_id = request.POST['exo']
    )
    score.score = request.POST['score']
    try:
      score.save()
    except:
      ret['type'] = 'error'
      ret['msg'] = 'error saving'
    return HttpResponse(json.dumps(ret), mimetype='application/json')

Should I use a hidden ModelForm or a manual Form/View ?

And what would it be like ?

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2011-10-06 09:50
    关注

    Why do you need form here? Send get / post request with score and exercise to some view and save the results there.

    And don't forget that anyone can make request with any data, so you need to think about some validation.

    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)