【问题标题】:Why am I getting an AttributeError?为什么我会收到 AttributeError?
【发布时间】:2017-11-18 04:26:46
【问题描述】:

我试图在点击submit 按钮时将用户的名字和姓氏存储在我的数据库中,这样下次当我手动签入Terminal 时,我可以准确地看到数据库中的内容用户输入。

这是我得到的错误:

我假设错误来自我的views.py 文件:

from django.http import HttpResponse
from .models import Person
from django.shortcuts import render

def index(request):
    if request.method == 'POST':
        first_name = request.POST.get('firstName')
        last_name = request.POST.get('lastName')
        if first_name and last_name:
            user = Person.objects.create(firstName=first_name, lastName=last_name)
            user.save()
    return render('request', 'music/index.html')

def detail(request, user_id): # Testing out page 2
    return HttpResponse("<h2>Page # (testing this out) " + str(user_id) + "</h2>")

【问题讨论】:

  • 不要发布错误信息的截图。点击“切换到复制粘贴视图”链接,然后在此处复制并粘贴文本。
  • @DanielRoseman 道歉。

标签: python django django-views


【解决方案1】:

代码将'request'(字符串文字)传递给django.shortcuts.render,它期望Request对象作为第一个参数。

传递视图函数的request参数:


return render('request', 'music/index.html')

应该是:

return render(request, 'music/index.html')

【讨论】:

  • 好的,这摆脱了AttributeError,但是每当我输入名字和姓氏并单击提交时,它都不会显示在我的数据库中。
  • @bojack,请发布一个包含详细信息的单独问题。
  • 这就是问题所在。如何让我的提交按钮工作,以便每当我输入名字和姓氏时,它都会出现在我的数据库中。
  • @bojack,不在评论中,而是在 NEW 问题帖子中。
  • 明白了,很抱歉,感谢您的解决方案。
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2016-08-21
  • 2014-07-04
  • 2020-06-06
相关资源
最近更新 更多