Django Forms保存数据的问题

时间:2018-12-12 17:12:29

标签: python django database model-view-controller

我正在尝试制作一个Django Web应用程序,该应用程序将有关用户及其兴趣爱好的信息保存到本地数据库中。由于某种原因,它无法正常工作,我在解决问题时遇到了麻烦。这是我的代码:

models.py文件:

my_choices = (
    (0, "None"),
    (1, "Football"),
    (2, "Cricket"),
    (3, "Swimming"),
    (4, "Cycling"),
)

class Hobby(models.Model):
    user = models.ForeignKey(Profile, on_delete=models.DO_NOTHING)
    field = models.IntegerField(choices=my_choices, default=0)

Profile的views.py函数:

def profile(request,user):

    try:
        profile_object = Profile.objects.get(id=user)

        if request.method.POST:
            form = HobbyForm(request.POST)
            if form.is_valid():
                profile_object.field = form.cleaned_data["field"]
                profile_object.save()

                context = {
                        "form": form,
                        "profile": profile_object,
                    }
                return render(request, 'mainapp/profile.html', context)

            else:
                context = {
                        "form": form,
                        "profile": profile_object,
                    }
                return render(request, 'mainapp/profile.html', context)
        else:
            context = {
                        "form": form,
                        "profile": profile_object,
                    }
            return render(request, 'mainapp/profile.html', context)

    except Profile.DoesNotExist:
        context = {
            "form": form,
            "profile": profile_object,
        }
        return render(request, 'mainapp/profile.html', context)

实际表单的forms.py代码:

class HobbyForm(ModelForm):
    class Meta:
        model = Hobby
        fields = ["field"]

还有profile.html页面:

<form action="myurl/{{profile.id}}/" method="post">
{% csrf_token %}
{% form.as_p %}
<input type="submit" value="OK">
</form>      

运行此代码时,出现以下错误:

TypeError at /profile/
int() argument must be a string, a bytes-like object or a number, not 'Member'
Request Method: GET
Request URL:    http://localhost:8000/profile/
Django Version: 2.1.3
Exception Type: TypeError
Exception Value:    
int() argument must be a string, a bytes-like object or a number, not 'Member'
Exception Location: C:\Users\install\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 965
Python Executable:  C:\Users\install\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.3
Python Path:    
['C:\\Users\\install\\Documents\\coursework\\Mumin',
 'C:\\Users\\install\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
 'C:\\Users\\install\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
 'C:\\Users\\install\\AppData\\Local\\Programs\\Python\\Python36\\lib',
 'C:\\Users\\install\\AppData\\Local\\Programs\\Python\\Python36',
 'C:\\Users\\install\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\Users\\install\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
Server time:    Wed, 12 Dec 2018 17:09:26 +0000

从本质上讲,我正在尝试做的是允许登录的用户添加他们感兴趣的兴趣爱好并将其保存到数据库中。爱好是在模型中预定义的。由于某种原因,此代码无法实现我想要的,并且我不确定为什么。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

<form action="myurl/{{profile.id}}/" method="post">在此行中,您尝试将id传递给view.py,但实际上,您没有这样做。正确的方法是

{% url 'yoururlnameassosiatedwithdesiredview' id=id %}

错误在这里出现:

profile_object = Profile.objects.get(id=user)