我是新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'參數即可。 –