我是新Django的,我做小SNS程序Django的__init __()得到了一个意想不到的关键字参数“内容”
我建立模型,并像下面的形式,
models.py
from django.db import models
class modelSNS(models.Model) :
content = models.TextField(blank = True)
password = models.CharField(blank = True, max_length = 10)
date = models.DateField(auto_now = True)
forms.py(我创造了这个文件)
from django.forms import ModelForm
from .models import modelSNS
class SNSForm(ModelForm) :
class Meta :
model = modelSNS
fields = '__all__'
和views.py我的应用程序是像下面
views.py
from django.shortcuts import render_to_response, render
from MySNS import models
from .forms import SNSForm
from django.http.response import HttpResponse, HttpResponseRedirect
def SNS_view(request) :
content = None
date = None
if request.method == 'POST' :
form = SNSForm(request.POST)
if form.is_valid() :
form = SNSForm(content=request.POST['content'], password=request.POST['password'])
#^ error occur in this line
form.content = request.POST['content']
form.save()
print(form)
return HttpResponseRedirect('/SNS/')
else :
form = SNSForm()
return render(request, 'SNS.html', {'form': form, 'content' : content, 'date' : date})
最后,我的html代码在这里
SNS。 html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SNS</title>
</head>
<body>
<form method="POST">
{% csrf_token %}
<p><label for="content">Content : </label><textarea id="content" name="content"></textarea></p>
<p><label for="password">Password : </label><input type="password" id="password" name="password"/></p>
<p><input type="submit" value="Submit" /></p>
<br/>
<h2>TimeLine</h2>
</form>
</body>
</html>
我要填写文本框“内容”和“密码”,并与点击发布到“提交”按钮
然后保存并打印文章我的网页上。
但是当我提交数据时,django引发TypeError。
我正在自学django,它对我来说如此痛苦。
请你告诉我,为什么这个错误发生,给我一些建议
一点意见会帮助我很多
在此先感谢
你为什么要创建'再次form'?顺便说一句,这不是你如何创建表单对象。第一种方法很好,你只要传递'request.POST'参数即可。 –