使用Django编程时如何编写表单操作

2024-10-08 18:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编写表单操作,从viwes.py调用newpost函数,newpost函数有两个参数newpost(request,myid),但是当我试图编写 action="{%url 'newpost' %}"出现如下错误:

TypeError at /newpostnewpost() 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>

这也是我的URL文件,如果我添加了任何参数,我是否应该更改它

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')
   
]

Tags: pathnameappgetobjectsrequestallpost
2条回答

在函数中发送myid时也发送myid

像这样“{%url'newpost'myid%}”

您的代码如下所示:

<form name="postbox" action="{%url 'newpost' myid %}" method="POST" align="center">

现在请参阅,创建polls/results.html模板:fromdjango doc

如图所示:

Django : HTML form action directing to view (or url?) with 2 arguments

在html中:

<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>

要将窗体的“操作”属性更改为: {%url 'newpost' myid %}

<form name="postbox" action="{%url 'newpost' myid %}" 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>

相关问题 更多 >

    热门问题