Django multiple files in one input can't be read by server - django

When I submit to Django a multiple files upload form
<input type="file" name="files" multiple />
I get a sensible result in request.FILES:
(MultiValueDict: {u'files': [(InMemoryUploadedFile: 0202.jpg (image/jpeg)), (InMemoryUploadedFile: 0203.jpg (image/jpeg))]})
But then my confusion starts. I thought request.FILES['files'] would contain a couple of files (appears to be a list), but it shows only
0203.jpg
No InMemoryUploadedFile part and more importantly: only the last file!
Looping through request.FILES with .iteritems() only goes over that one file too, request.FILES['files'] is distinctly uniterable; the below code actually froze up the console and made it beep endlessly:
for v in request.FILES['files']:
print v
print type(v)
So... Is any of this this normal? What am I doing wrong?
As I'm starting to think this may be a bug: I'm using Django 1.4.2 with Python 2.7 on Windows 7.

It turns out the answer was on Stack Overflow after all, on a slightly different (client side, at least) problem here: multiple files upload using same input name in django
Turns out that request.FILES['files'] was wrong and should have been request.FILES.getlist('file').
Why this is the case is a complete mystery to me. If anyone can answer with an explanation, I'll gladly switch the accepted answer.

Related

content empty when using scrapy

Thanks for everyone in advance.
I encountered a problem when using Scrapy on Python 2.7.
The webpage I tried to crawl is a discussion board for Chinese stock market.
When I tried to get the first number "42177" just under the banner of this page (the number you see on that webpage may not be the number you see in the picture shown here, because it represents the number of times this article has been read and is updated realtime...), I always get an empty content. I am aware that this might be the dynamic content issue, but yet don't have a clue how to crawl it properly.
The code I used is:
item["read"] = info.xpath("div[#id='zwmbti']/div[#id='zwmbtilr']/span[#class='tc1']/text()").extract()
I think the xpath is set correctly and I have checked the return value of this response and it indeed told me that there is nothing under this directory. Results shown here:'read': [u'<div id="zwmbtilr"></div>']
If it has something, there should be something between <div id="zwmbtilr"> and </div>.
Really appreciated if you guys share any thoughts on this!
I just opened your link in Firefox with NoScript enabled. There nothing inside the <div #id='zwmbtilr'></div>. If I enable the javascripts, I can see the content you want. So, as you already new, it is a dynamic content issue.
Your first option is try to identify the request generated by javascript. If you can do that, you can send the same request from scrapy. If you can't do it, the next option is usually to use some package with javascript/browser emulation or someting like that. Something like ScrapyJS or Scrapy + Selenium.

django - ckeditor bug renders text/string in raw html format

am using django ckeditor. Any text/content entered into its editor renders raw html output on the webpage.
for ex: this is rendered output of ckeditor field (RichTextField) on a webpage;
<p><span style="color:rgb(0, 0, 0)">this is a test file ’s forces durin</span><span style="color:rgb(0, 0, 0)">galla’s good test is one that fails Thereafter, never to fail in real environment. </span></p>
I have been looking for a solution for a long time now but unable to find one :( There are some questions which are similar but none of those have been able to help. It will be helpful if any changes suggested are provided with the exact location where it needs to be changed. Needless to say I am a newbie.
Thanks
You need to mark the relevant variable that contains the html snippet in your template as safe
Obviously you should be sure, that the text comes from trusted users and is safe, because with the safe filter you are disabling a security feature (autoescaping) that Django applies per default.
If your ckeditor is part of a comment form and your mark the entered text as safe, anybody with access to the form could inject Javascipt and other (potentially nasty) stuff in your page.
The whole story is explained pretty well in the official docs: https://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping

chrome after post forms ,try to get another captcha image?

I'm using the django-simple-captcha in django forms,i used it in a
subclass of sessionwizardform,and in the last wizard form,i put a captcha field in it .
at first ,things goes all right ,but after few days ,may be by the duplication of the stale files i installed. in a word ,when i test the wizard form .In the chrome ,the captcha displays pic well,but i fill the right checking words ,in the last post of the form ,it did not post the data,and get another pic for you to validate.
and i found some answer by my previous question ,but i still can not solve it ,the help url is https://github.com/mbi/django-simple-captcha/issus/41 ,and my previous question url is django-simple-captcha-keeps-saying-invalid-captcha .
in it ,i described the detail of my django-simple-captcha.
likewise ,i'm still not know much about the globally and the virturenv, I have the django-simple-captcha uninstalled and reinstalled ,have the django uninstalled and reinstalled ,
...
but none of these worked ,and i do not re-defined the CAPTCHA_OUTPUT_FORMATin my settings.in the chrome ,i tested like
this
<td width="70%" class="text-left">
<img src="/captcha/image/d61b817a32d5aee07a4c2f8ce3cf71ccc7dea85f/" alt="captcha" class="captcha">
<input class="form-control" id="id_xy_assist-captcha_0" name="xy_assist-captcha_0" type="hidden" value="d61b817a32d5aee07a4c2f8ce3cf71ccc7dea85f">
<input class="form-control" id="id_xy_assist-captcha_1" name="xy_assist-captcha_1" type="text">
</td>
and i think this is allright,but in the chrome after all the fields
post ,there is another get captcha image which make this wizard form data can never be saved.and without this captcha field ,everything is right .
and comparing with the issues/41 in the url.
my test of captcha has two Internal Server Error,one for /test/,one for /test-modelform/.
and there is not any captcha directory except this one ?
could any one help me find a way to solve it ?
i am usint python 3.3 ,and i'd like django-simple-captcha reserved,which is really simple.
after hours checking codes and testing ,i found my programming is very poor ,i canot resolve this question ,and since i 'm a newby.
nearly tried all ways ,i solved it by this :
i copy his code to the newest django-simple-captcha's fields.py and reserves the classclass BaseCaptchaTextInput,
but the test in the captcha is not passed ,and i ignore it.
and i think my python's class has not well understand , and have to spend time on it to improve myself.

Dajaxice: Having trouble getting Dajaxice working

I know, I know, the first thing you are thinking is to check all of the instructions online and on dajaxproject.com
Well I have checked and checked and rerun this installation and I cannot figure out what the problem is. I assume it has something to do with the version of Dajaxice I am using and the version the installation instructions are, but those details are usually missing.
At this point in the process, I have everything setup to accomplish a very simple task. Load a webpage from the IP:8000 (ie 1.2.3.4:8000) and have that page load a simple html form button. Once that button is pressed, the page should then send a mesage (using dajaxice) to the server, and once it returns an alert box will pop up, proving that the dajaxice install is working.
So, I have:
Installed Dajaxice (and dajax for that matter), by getting the zip from here: github [dot] com/jorgebastida/django-dajaxice/
Changed everything in a basic Django project to follow the details in this tutorial: http://django-dajaxice.readthedocs.org/en/latest/installation.html
I have setup all of the details of ajax.py, and index.html to work with this example: django-dajaxice [dot] readthedocs [dot] org/en/latest/quickstart.html
PS Sorry for the "[dot]"s, but being a new registered user they limit you to 2 per post, so I tried to give you the best links clickable.
I am stumped. I have been trying many different things, but the documentation available seems so sporadic. It's gotta be something simple I am messing up, but I cannot seem to find it.
Currently, the "Say Hello!" button shows up on the screen, but nothing happens when you press it. Any ideas?
Here is a link to the zip file of the Django project I am working with here: https://docs.google.com/open?id=0B5rzYk6W5HlrYk9veVRfM3JkTGc
The way I have it all organized, DjangoAjax is just the folder containing the Django project. ajax_prac is the project, and practice is an app. Also you'll see a folder called django_media...that's pretty self explanatory, it just holds all the "media" for my Django projects, including jquery.js and jquery.dajax.core.js
If there are some details that I have smudged in the code, contrary to the installation guide, I apologize. I have been working through so many iterations of this, that it is likely I could've forgotten to change something back when making this post.
Can anyone help?
All the documentation weirdness is probably related with the new release I made yesterday (8 hours ago). Probably you was using an old version of dajaxice reading the new documentation.
I've release django-dajaxice (0.5) and django-dajax (0.9.1), both fully compatible with the last version of Django (1.4.1).
django-dajaxice: http://pypi.python.org/pypi/django-dajaxice
django-dajax: http://pypi.python.org/pypi/django-dajax
For this release I've also rewrite both documentations and hosted them on readthedocs. Also, http://dajaxproject.com code is now opensource so everybody can read the code and see how dajaxice works in production.
django-dajaxice: http://django-dajaxice.readthedocs.org/en/latest/
django-dajax: http://django-dajax.readthedocs.org/en/latest/
example project: https://github.com/jorgebastida/dajaxproject.com
Could you please upgrade to django-dajaxice (0.5) follow the new installation steps (quite similar http://django-dajaxice.readthedocs.org/en/latest/installation.html) and post here any traceback/issue you could have?
Thanks
In your example code ( DjangoAjax / ajax_prac / templatedirectory / practice / index.html):
<input type="button" value="Send message to server!"
onclick="Dajaxice.ajax.sayhello(my_js_callback);">
Should be:
<input type="button" value="Send message to server!"
onclick="Dajaxice.ajax_prac.practice.views.sayhello(my_js_callback);">
I.e. It must reference a Python module that contains your registered Dajaxice view. And according to your sample code, ajax_prac.practice.views exists while ajax.sayhello does not.
Also the Python module must be prefaced with "Dajaxice" (which is probably a significant source of confusion).

Django admin file upload doesn't work, how do I debug this?

I'm on Mac Leopard. Trying to get django admin file uploads, or really image uploads to work. In my app's models.py I set the field to:
image = models.FileField(upload_to='images', max_length=500)
(started w/ ImageField, but thought if a file doesn't work, then the image for sure won't work)
It says the upload happened. Gives me a positive result. Saves the path to my database. But looking for the file, well, it isn't there.
Since there's no error message, it's hard to debug.
Tried various permissions, but nothing is working.
Any ideas?
Is your MEDIA_ROOT set correctly? http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
From the comments it sounds like you found a solution to your issue. For future reference, you may want to check out django-debug-toolbar to help you troubleshoot problems like this (Especially when there's no error, it just doesn't work right!). Code available here: http://github.com/robhudson/django-debug-toolbar/tree/master