【发布时间】:2021-06-29 21:57:28
【问题描述】:
我想编写表单操作以从 viwes.py 调用 newpost 函数,newpost 函数有 2 个参数是 newpost(request,myid),但是当我尝试编写
action="{%url 'newpost' %}" 出现如下错误:
TypeError at /newpost
newpost() missing 1 required positional argument: 'myid'
我怎样才能在表单动作中发送这个参数? 请告诉我
def newpost (request,myid):
blockedperson=[1]
assert isinstance(request, HttpRequest)
print("if1")
print (request.POST.get('postcontent'))
print (type(request.POST.get('postcontent')))
while request.POST.get('postcontent'):
print ("if2")
if myid not in blockedperson :
savepost=post()
savepost.person_id= 437819147
savepost.content=request.POST.get('postcontent')
savepost.p_date=dt.datetime.now()
savepost.save()
else :
blocked="sorry, you are blocked, you can not share a post, for more information contact with IT on 437819147@kku.edu.sa"
return render (request,'home.html',{'block':blocked})
allpost=post.objects.all()
allperson=person.objects.all()
allstudent=student.objects.all()
allteacher=teacher.objects.all()
allcomp=company_rep.objects.all()
return render (request,'home.html',{'posts':allpost , 'person':allperson,'student':allstudent,'teacher':allteacher,'comp':allcomp,'id':myid})
<form name="postbox" action="{%url 'newpost' %}" method="POST" align="center">
{% csrf_token %}
<label>shear your ideas, information, and experience...</label>
<br />
<textarea id="post" name="postcontent" rows="4" cols="50">
</textarea>
<br />
<input style="width: 31%;" type="submit" value="post" name="postsubmit">
</form>
这也是我的 urls 文件,如果我添加任何参数,我应该在其中更改吗?
urlpatterns = [
path('' , app.views.login),
path('newpost' , app.views.newpost,name='newpost'),
path('signup' , app.views.signup,name='signup'),
path('signupteacher' , app.views.signupteacher,name='signupteacher'),
path('signupstudent' , app.views.signupstudent,name='signupstudent'),
path('signupcompany' , app.views.signupcompany,name='signupcompany')
]
【问题讨论】:
-
可能的答案可以在这里找到,stackoverflow.com/questions/42440962/… 你有带参数的写操作,
-
请正确解释场景。