【问题标题】:how to write form action when programing with django使用 django 编程时如何编写表单动作
【发布时间】: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')
   
]

【问题讨论】:

标签: python html django


【解决方案1】:

在发送 myid 时,也可以在函数中发送它

喜欢这个“{%url 'newpost' myid %}”

您的代码将如下所示:

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

参考现在,创建一个 polls/results.html 模板:来自django doc

【讨论】:

  • 我试过这个但是出现一个错误是:NoReverseMatch at /Reverse for 'newpost' with arguments '('',)' not found. 1 pattern(s) tried: ['newpost$']我调用newpost函数是这样的:``` return newpost(request,int(myid)) ```当我写参数myid时,它在html页面中出现问题
  • 我的 urlpatterns 是这样的:path('newpost' , app.views.newpost,name='newpost') 有问题吗?
  • myid 是从哪里来的???您没有将 myid 从视图传递给模板
  • 这是对不完整问题的不完整答案。\
【解决方案2】:

在这里找到:

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>

您想将表单的 action 属性更改为: {%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>

【讨论】:

  • 我试过这个但是出现一个错误是:NoReverseMatch at /Reverse for 'newpost' with arguments '('',)' not found. 1 pattern(s) tried: ['newpost$']我这样调用newpost函数:```return newpost(request,int(myid))```和它当我写参数myid时在html页面中出现问题
  • 我的 urlpatterns 是这样的:path('newpost' , app.views.newpost,name='newpost') 有问题吗?
  • 这是对不完整问题的不完整答案。
  • @AvishkaDamdawinna 仔细检查你是对的,他没有将 myid 传递给实际模板,但他已经传递了未在视图函数中设置的“id”,所以基本上该函数是预期的您将一个值传递给它,而不是将它传递回已呈现的内容,因为该值不存在....据我所知,要检查这一点,我可能会专注于尝试查看传递给模板的 id通过在浏览器中检查请求并确保它是预期的,然后按照上面的过程进行操作
猜你喜欢
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 2018-05-10
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
相关资源
最近更新 更多