MultiValueDictKeyError / request.POST

2024/11/13 14:14:00

I think I hav a problem at request.POST['title']

MultiValueDictKeyError at /blog/add/post/"'title'"Request Method: GETRequest URL: http://119.81.247.69:8000/blog/add/post/Django Version: 1.8.2Exception Type: MultiValueDictKeyErrorException Value:
"'title'"Exception Location: /usr/local/lib/python2.7/dist- packages/django/utils/datastructures.py in getitem, line 322Python Executable: /usr/bin/pythonPython Version: 2.7.3

views.py

def add_post(request):entry_title = request.POST["title"]return HttpResponse('Hello %s' % entry_title)

write.html

<form method="POST" action="/blog/add/post/">
<p><label for "title">Title</label><input type="text" id="title" name="title" value="" />
</p>
<p><label for 'category'>Category</label><select id="category" name="category"></select>
</p>
<p><label for 'tags'>Tags</label><input type="text" id="tags" value="" />
</p>
<p><textarea id="content" name="content"></textarea>
</p>
<p><input type="submit" value="Write" />
</p>

Answer

Change:

def add_post(request):entry_title = request.POST["title"]return HttpResponse('Hello %s' % entry_title)

to:

def add_post(request):entry_title = request.POST.get("title", "Guest (or whatever)")return HttpResponse('Hello %s' % entry_title)

and it won't throw a KeyError, but you should look at using Django's forms rather than pulling values directly from the POST data.

Alternatively, you can keep your existing code and simply check for the exception:

def add_post(request):try:entry_title = request.POST["title"]except KeyError:entry_title = "Guest"return HttpResponse('Hello %s' % entry_title)

but this is what .get() does internally already.

https://en.xdnf.cn/q/72080.html

Related Q&A

How can I auto run py.test once a relative command has been change?

Via autonose or nosy, it will automatically run the nosetests once the some tests file or the relative files have been changes. I would like to ask that whether py.test provides the similar function fo…

Publish a post using XML-RPC WordPress API and Python with category

Im doing a migration from a website to another one which use Wordpress. I created new custom types for my needs (with the plugin Custom Post Types), and I created categories for each custom type.I then…

Django registration email not sending

Ive been trying to get the django-registration-redux account activation email to send to newly registered users.Ive gotten all non-email related parts to work, such as loggin in/out and actually regist…

NumPy data type comparison

I was playing with comparing data types of two different arrays to pick one that is suitable for combining the two. I was happy to discover that I could perform comparison operations, but in the proces…

A simple method for rotating images in reportlab

How can we easily rotate an image using reportlab? I have not found an easy method. The only way found comes from http://dods.ipsl.jussieu.fr/orchidee/SANORCHIDEE/TEMP/TEMP_LOCAL/cdat_portable/lib_new…

XML header getting removed after processing with elementtree

i have an xml file and i used Elementtree to add a new tag to the xml file.My xml file before processing is as follows <?xml version="1.0" encoding="utf-8"?><PackageInfo …

How to ntp server time down to millisecond precision using Python ntplib?

I am creating a python module that will output the time from a selection of NTP Pool servers to millisecond precision as an exercise in showing how server timestamps vary. Thus far I have been able to …

Control 2 separate Excel instances by COM independently... can it be done?

Ive got a legacy application which is implemented in a number of Excel workbooks. Its not something that I have the authority to re-implement, however another application that I do maintain does need t…

nested Python numpy arrays dimension confusion

Suppose I have a numpy array c constructed as follows:a = np.zeros((2,4)) b = np.zeros((2,8)) c = np.array([a,b])I would have expected c.shape to be (2,1) or (2,) but instead it is (2,2). Additionally,…

how to reuse tests written using unittest.testcase

Ive written some tests using unittest as below and I want to reuse them in another class where Im stuck and need help.. Code snippets are as below.MyTestClass.pyClass MyTestClass(unittest.TestCase): …