0

i want to write form action to call newpost function from viwes.py , newpost function has 2 arguments which are newpost(request,myid), but when i tried to write action="{%url 'newpost' %}" an error appears like this :

TypeError at /newpost newpost() missing 1 required positional argument: 'myid'

how can i send this argument in form action ? please tell me


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>

this also my urls file , should i change in it if i add any arguments ?

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')
   
]
hejazi
  • 13
  • 2

2 Answers2

3

send myid in action also as you are sending it on function

like this "{%url 'newpost' myid %}"

your code will look like:

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

refer Now, create a polls/results.html template: from django doc

Keval
  • 557
  • 10
  • 15
  • i tried this but an error appears that is : `NoReverseMatch at /` `Reverse for 'newpost' with arguments '('',)' not found. 1 pattern(s) tried: ['newpost$']` i call the `newpost` function like this: ``` return newpost(request,int(myid)) ``` and it make a problem in html page when i write the argument `myid` – hejazi Apr 03 '21 at 11:02
  • my urlpatterns like this : ```path('newpost' , app.views.newpost,name='newpost')``` is there a problem in it ? – hejazi Apr 03 '21 at 11:51
  • Where's the myid coming from??? you're not passing `myid` to the template from the view – Avishka Dambawinna Apr 03 '21 at 13:32
  • This is an incomplete answer to an incomplete question.\ – Avishka Dambawinna Apr 03 '21 at 13:54
0

As found here:

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

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

You want to change the action attribute of the form to: {%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>
Kwsswart
  • 529
  • 1
  • 7
  • 20
  • i tried this but an error appears that is : `NoReverseMatch at /` `Reverse for 'newpost' with arguments '('',)' not found. 1 pattern(s) tried: ['newpost$']` i call the newpost function like this: ``` return newpost(request,int(myid)) ``` and it make a problem in html page when i write the argument `myid` – hejazi Apr 03 '21 at 11:06
  • my urlpatterns like this : ```path('newpost' , app.views.newpost,name='newpost') ``` is there a problem in it ? – hejazi Apr 03 '21 at 11:50
  • This is an incomplete answer to an incomplete question. – Avishka Dambawinna Apr 03 '21 at 13:54
  • @AvishkaDamdawinna on closer inspection you are right he hasn't passed myid to the actual template, but he has passed 'id' which hasn't been set within the view function so essentially the function is expecting you to pass a value into it not pass it back what is has rendered as this value is not in existance.... As far as i can understand, to check this i would focus on trying to view the id passed to the template perhaps by checking the request in browser and make sure it is whats expected and then once it is follow the procedure above – Kwsswart Apr 04 '21 at 07:34