I have multiple forms on one page and my view is handling these by checking the value of the submit. This seems to work all fine, however on of my forms is getting the error below.
'QueryDict' object has no attribute 'method'
VIEW
def all(request):
if request.method == 'POST':
if 'all' in request.POST['submit']:
all(request.POST)
elif 'addtype' in request.POST['submit']:
addtype(request.POST)
elif 'addnewpm' in request.POST['submit']:
addnewpm(request.POST)
elif 'addnewspec' in request.POST['submit']:
addnewspec(request.POST)
elif 'update' in request.POST['submit']:
update(request.POST)
elif 'addnewrecord' in request.POST['submit']:
addnewrecord(request.POST)
Basically I am just passing the post values to seperate functions based on which submit button was pressed. They all work fine except for the first one 'all'. The 'all' submit button is submitting a large amount of data, and I can see all this data in the traceback.
Maybe it has something to do with my HTML code.
<table class="gridtable">
<tr>
<td class="topheader-left" colspan="10">
<form action="" method="post">
<button type="submit" value="all" name="submit" style="border:0px;">
<img src="{% get_static_prefix %}images/update.png" style="width:27px;height:27px;">
</button>
</td>
</tr>
Below this I just have a large number of table cells with fields and a /form at the end.
The code from one of the forms on my page that works fine.
<table width="100%">
<tr>
<form method="post" action="">
<td>
<input id="newtype" type="text" name='newtype' size="40" value="Service Type">
</td>
<td>
<button name="submit" type="submit" value="addtype" style="border:0px;">
<img src="{% get_static_prefix %}images/Add-icon.png" width="20" height="20" border="0">
</button>
</td>
</form>
This form seems to work fine. I don't understand what I am doing differently.
Cheers guys.
Seems like a simple function name collision. Your view method name is all and you're calling all(request) again and again, and again :) if the value of submit == all.
Using in to look for the value of submit in request.POST seems odd. Why not just set the value once and compare it that way?
submit = request.POST['submit']
if submit == 'all':
# call method
elif submit == 'addtype':
# etc
Related
I have a form for a voting system. I have the candidates in a table row. At the end of the row I have button with the candidate ID
<form method="post">
<table>
...
<tr>
<td>name</td>
<td>campaign promises</td>
<td><button id="candidate_id"></td>
<tr>
...
</table>
</form>
"candidate_id" is an actual number and unique. I would like to get that id value from the post request, so I know which candidate was selected. Im hoping its as simple as
request.POST.Something
Yes, you can provide name attribute to the button, say for example candidate_id_value as:
<form method="POST">
<table>
...
<tr>
<td>name</td>
<td>campaign promises</td>
<td><button id="candidate_id" name="candidate_id_value" ></td>
<tr>
...
</table>
</form>
Then access this value in the view as:
request.POST.get("candidate_id_value",False)
I am trying to pass what ever the user inputs into the url as keyword arguments (even if its not a real entry). When I try to assign the input name as the keyword arguments it fails.
HTML:
<p class="search">
<form method="GET" action="{% url 'job' %}" class="sidebar-form">
<div class="ogsearchbar input-group">
<input class="searchbarz" type="text" name="user_input" id="user_input" placeholder="Enter Job Number" autocomplete="off" />
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</p>
Django:
def get_job(request):
if request.method == 'GET':
formvar = request.GET['user_input']
return HttpResponseRedirect('/jobs/' + formvar)
What you want doesn't make sense. The {{ }} signs denote a context variable which is passed into the template from the server, before the template is rendered. But you're trying to use a value which is only defined when the user actually types something into the rendered page itself.
You could probably do this with some Javascript, but there doesn't seem to be much point. Drop the parameter from the URL and let the form send it in the query params, which you can access in your view as request.GET.
I decided to just go with javascript since my django code was not working with a request method when refreshing the page.
I am using python and flask to make a web app. I am new to it, but have gotten most of what I am trying to accomplish done. Where I am stuck, is that I have a label whose value is a python variable( {{id}} ) This id is the id of a row I need to update in a sqlite database. My code is below. when I click the approve button, it takes me to a route which does the update query, but I have no way to pass the {{id}} with it. This would have been much easier if I could have just used javascript for the update query, but everything I've found using javascript, is for web sql, not sqlite, even though some of them say they are for sqlite.
</script>
<table border='1' align="center">
{% for post in posts %}
<tr>
<td>
<label>{{post.id}}</label>
<h1 id ='grill1'>{{post.photoName}}</h1>
<span>
<img id = 'photo1' src='{{post.photo}}' alt="Smiley face" height="200" width="200">
</span><br>
<h5 id ='blurb1'>
{{post.blurb}}
</h5>
<br>
<div style=" padding:10px; text-align:center;">
<input type="button" value="Approve" name="approve" onclick="window.location='/approve;">
<input type="button" value="Deny" onclick="window.location='/deny';"><br>
</div>
</td>
</tr>
{% endfor %}
</table>
Why not just do:
...
<input type="button" value="Approve" name="approve" onclick="window.location='/approve/{{post.id}};">
<input type="button" value="Deny" onclick="window.location='/deny/{{post.id}}';">
...
Then your flask route for approve and / or deny can just take a parameter for the post to approve or deny. i.e.:
#app.route("/approve/<int:post_id>")
def approve(post_id):
"""approve this post!"""
I am new to Python, GAE and the datastore model. So there are lots of things which I do not know yet, so please be patient :)
I am working on a web service that allows people to post 'name' and 'desc' (description) of an item and it will be included in a table on the same page. However when I clicked the submit button I got the error: 404 Not Found, The resource could not be found.
I am expecting a lot of things to be wrong in my code shown below (I only include short snippets of my code which I think is relevant to make it easier for reading), and my biggest problem is I have no idea which parts are wrong or which specific questions to ask. But I hope I can use this chance to learn more about everything that's involved in my code (Jinja, HTML, GQL etc), and how I can fit them all together.
class Events(ndb.Model):
name = ndb.StringProperty()
desc = ndb.StringProperty()
class Promote(webapp2.RequestHandler):
def get(self):
query = ndb.gql("SELECT * "
"FROM Events "
)
template_values = {"events" : query,}
template = jinja_environment.get_template('promote.htm')
self.response.out.write(template.render(template_values))
def post(self):
event = Events(name = self.request.get('name'), desc = self.request.get('desc'))
event.put()
self.redirect('/promote')
app = webapp2.WSGIApplication([('/', Main),
('/publicsearch', PublicSearch),
('/promote', Promote)],
debug=True)
This is my html code for that page.
<div class="jumbotron">
<div class = "container">
<form action="/promote" method="post">
<fieldset>
<div class="row-fluid">
<p> Promote your event here! </p>
<div class="row-fluid">
<div class="span6">
<p> Name of event: <br>
<textarea class="input-block-level" name="name" rows="1" cols = "50"> </textarea></p>
<p> Event description: <br>
<textarea class="input-block-level" name="desc" rows="3" cols = "50"> </textarea></p>
<p><input type="submit" value="Submit">
</div>
</div>
</div>
</div>
</div>
<h4> Events feed </h4>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for event in events %}
<tr>
<td>{{ event.name }} </td>
<td>{{ event.desc }} </td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
Your form is trying to post to a handler with a url of /wishlist however the only handler for POST methods you have registered is for /promote.
These things need to match up. Either change the form or the handler mapping.
Also while you are at it check that your app.yaml makes sense. Have a look in the logs whilst you are at it, you will see what URL is being requested.
I can't get this to work: I navigate to the page I want, but I'm doing something wrong with the variables it seems.
views.py:
#view_config(http_cache=1,route_name='hoofdpagina', renderer='templates/hoofdpagina.pt')
def hoofdpagina(request):
page = DBSession.query(MyModel) #.filter_by(id='1').all()
if 'form.submitted' in request.params:
name= request.params['name']
page2=Page(name)
DBSession.add(page2)
return HTTPFound(location=request.route_url('view_page',pagename=name))
return dict(page=page)
#view_config(route_name='diagnose', renderer='templates/diagnose.pt')
def diagnose(request):
return request
kak = ['test1','test2','test3t']
content = {"test1","test2","test3"}
return {'content' :content, 'test' :kak}
hoofdpagina.pt:
<form class="span12" action="/diagnose" method="POST">
<table class="table table-hover">
<thead class="header">
<tr>
<th>D Nr.</th>
<th>Datum</th>
<th>Patient</th>
<th>Prior</th>
</tr>
</thead>
<tr tal:repeat="Page page" >
<td tal:content="Page.dosiernummer"></td>
<td tal:content="Page.dosiernummer"></td>
<td tal:content="Page.datum"></td>
<td tal:content="Page.naamPatient"></td>
<td tal:content="Page.prioriteit"></td>
</tr>
</table>
</form>
<form action="/diagnose" method="post">
<input type="submit" value="Save" name="form.submitted" ></input>
<label name="name">et werkt slet</label>
</form>
I can show all the variables of page in my table. But when I press the submit button I can't get the content of the "name" label to my diagnose page. I don't know how I can show the value.
ps: question is based on this post: Pyramid app: How can I pass values into my request.route_url?
Instead of you'd need an input of some sort:
<input type="text" name="name" value="et werkt slet">
or type="hidden" if you don't want it displayed.
I'm still learning Pyramid as well, but I also wonder if from your code that the submit action is going to post to def hoofdpagina anyway. I think you might have to move your POST handling to:
#view_config(route_name='diagnose', renderer='templates/diagnose.pt')
def diagnose(request):
if 'form.submitted' in request.params:
name= request.params['name']
page2=Page(name)
DBSession.add(page2)
return HTTPFound(location=request.route_url('view_page',pagename=name))
a sollution is working with url dispatching like this:
_init_.py:
config.add_route('diagnose1', '/diagnose1/{dosierid}')
views.py
#view_config(route_name='diagnose1', renderer='templates/diagnose.pt')
def diagnose1(request):
tabeldata=''
dosierid = request.matchdict['dosierid']
now you have your id from one view to another.
In the init.py you have to add url dispatching (config.add_route).
Your can get the data out of the url and pass on to another page like this.