Django not displaying your Images ? [Solved]

~/Untitled.html


Getting your images to display on Django.

You might have found it a bitch to get your images ( background, etc ) to display on django – even after setting MEDIA_ROOT and MEDIA_URL to the seemingly proper values.

Here’s how you actually do it –

go to your urls.py and add this urlconf pattern at the end :

(r’^site_media/(?P<path>.*)$’, ‘django.views.static.serve’,{ ‘document_root’: ‘/home/tnevzn/media/’ })

so, your pics should now reside under /home/tnevzn/media.
and to access your pics , e.g: <body background="/site_media/a.jpg"> , where a.jpg resides in /home/tnevzn/media.

That’s all. No values in MEDIA_URL, MEDIA_ROOT or the like in settings.py are necessary – leave them blank.

Holy fuck – I can’t believe this wasn’t there ANYWHERE on the fuckin’ net. I wasted like 30 minutes doubting my cognitive skills by reading all the sites that said the same thing – MEDIA_ROOT and MEDIA_URL.

Damnit. You gotta fend for yourself :(

- anonymouscowherd

Leave a Comment

Python Decorator Functions Explained through an example of security.

~/Untitled.html


Python Decorators in action

I never really knew or understood why we actually needed python decorator functions in the first place. But after a brief stint with django, I do.

One of the good reasons to use decorators is for a purpose called Aspect Oriented Programming – those of you who know and use the Spring framework might be familiar with it – it’s the practice of isolating various Aspects of your job into a seaparate MOdule , or function. E.g: SEcurity, LOgging, etc.

If all of your function require that a user be logged in before he be able to execute that function – you can take two ways – check whether the user is blacklisted in every function, by writing 2-3 lines of code, or you can use decorator functions like this :

def check_whether_user_is_blacklisted(func):
    def inner(*args, **kwargs):
        if kwargs['username'] == 'Ramm':
            print "Sorry, Ramm is not allowed on our site."
        else:
            func(*args, **kwargs)
        return inner
@check_whether_user_is_blacklisted
def myfunc(username="Stein"):
print "Hey %s, you're logged in !" % ( username)
myfunc("test")
myfunc("Ramm")

Note the @ syntax – similar to java – was adopted after python 2.4 ( we’re in the age of python 3k, so no worries – you probably have python 2.4 installed. )

the myfunc ( a function object ) is automatically passed to check_whether_user_is_authenticated as the argument, and that function instead replaces myfunc with another function defined inside it – inner.

The inner function, as you can see – applies an additional check to see whether the user is blacklisted or not. If not, it executes the original function myfunc. If he is blacklisted, the function prints a message and exits.

so,

myfunc(“test”) – will print “Hey test, you’re logged in!”
myfunc(“Ramm”) – will print, “Sorry, Ramm is not allowed on our site.”

So, just by adding a line “@check_whether_user_is_blacklisted” before any of our functions that we don’t want blacklisted users to be able to execute, we can achieve this effect.

[  Courtesy SmileyChris ]:

Here’s another hint, use “wraps” to make the decorated function retain the docstring and any other view attributes:

from django.utils.functional import wraps
def check_whether_user_is_blacklisted(func):
    @wraps(func)
    def inner(*args, **kwargs):


Happy decorating !

-anonymouscowherd

Comments (3)

A complete Idiot’s guide to quitting your job.

Are you an idiot working nights doing stuff you don’t give a shit about, that any Tom, Dick or Harry could do ? Are you wasting your skillset and rusting away at a job where you know the work will never be good enough to match your ambitions or skills ?
Did you know that even bacteria are being genetically altered to solve the Hamiltonian Path Problem ? Does your work compare to this ?
If not, you just got trumped. By bacteria, for crying out loud. And you know it’s time to quit when you get trumped by bacteria.

But how ? You’ve reached the right place to answer that question. This guide will help you quit that meaningless job in a way that’s less painful, and requires lesser time than you would have imagined.

Of course, as you might have guessed – it comes at a price. The price being your undiverted attention to what you want to do in life – for 2 months.
Here’s the technique I’ve developed to help myself quit my job, within 2 months –

1. Starting today, I’ll imagine every moment outside the office as if I no longer have any job.
2. Then, in that time, I’ll experiment with what I want to do in life, every moment. Now, I have an idea of the things I’m interested in – like computer science, electronics, biology, physics, music. If you’re not – then this period will be more fun for you, since you will get to try as many new things as possible within this period. And out of these things, whatever pleases your fancy the most , whatever inspires you the most – that’s your life !

Continue doing this for 2 months, or more, depending on how familiar you are with your goals in life – with UNDIVERTED attention. No wasting time on girls, games, pr0n, soaps, movies, hanging out. Think of this period as your “coccoon period” when you must cut yourself off from the world in order to emerge a better living being.

That is the price.

At the end of the 2 month period, you will have a better idea of what you want to do in life, and if you already have the idea sooner than two months, then in the rest of the time – invent new job oppurtunities for yourself within that area – basically grow comfortable with the idea that you can make a nice living in that area, alongside enjoying yourself.
If you need 3 months – so be it. 4 months – fine. It’s a variable period that can change from person to person. I have given myself 2 months.

Try it, I’m very confident this will work !

Leave a Comment

To all graduating students, and existing blue-collar slaves.

It’s been an year since you graduated and got your bachelors’ degree in an engineering field from some reputed college in your nation. Well, save the applause. What’s more, you got into one of the ‘best companies that one can get into’, with one of the highest paychecks. What more could you want ? And why do the clouds of impending doom loom large upon this post so far ? Here’s why :

All the top companies that come to the colleges of smaller nations are actually baiting you. They come to your colleges with presentations detailing how they do this and they do that using ‘cutting-edge’ technology, they prey on your ego with stuff like – ‘hey, you made it through ! You’re Awesome! We need you !” – well, you’re a dumbass. That’s what you are. If you’re not in an American college, and you got hired as a bachelor, or a masters’ graduate for an American MNC, you were duped. You’ll soon be working in what can be best described as a sweatshop factory of the software industry.

Here’s the great American MNC ploy :

1. Setup a sweatshop factory ( a branch of the MNC ) in the smaller nation. Call it fancy names – “The biggest centre outside the US”, “A major undertaking”, etc.

2. Lure the local bright minds, so that you can pay them peanuts in their own currency, as opposed to the higher salary you’d have to pay them if they came to the US.

3. Make them grow comfortable with the amenities you provide, and all the while condition them and their minds to do mediocre work over a span of years so that they no longer have either the readily available skill or the drive to gain that skill. – Their only option – continue to do this dreadful work.

4. Make all the research and intellectually stimulating work happen in the US, and discard the residual garbage, mundane work to the smaller nation. At a fraction of the price.

If you believe me not, be my guest – give it a try. Pat yourself on the back to have gotten into the industry leaders, the pioneers, the bigwigs.  Just as an experiment.

I weep for the future of the smaller nations where people give up their prime to serve smaller causes and doing mundane, sweatshop work. It’s never too late to quit. Quit your sweatshop, IT job. Think. Have some self-esteem. What do you effectively do when you join an MNC ? You subconsciously tell yourself : “Hey, I’m not bright enough to make money on my own, so let’s do someone else’s bidding and work for an idea that belongs to someone else. “

Come on, do justice to the stochastic calculus, the genetic algorithms, the computational biology, the chemistry, the design, the fluid mechanics that you were taught. Or would you rather write a script to upload data in an XML file to a database ( or something that requires even lesser intellectual capacity ) , when intriguing problems in almost all scientific areas exist ?

Here’s what 99% of the smaller nation hires are doing at top companies :

- Fixing bugs in code written by other people. More often than not, the ‘bug’ is a wrong error message that you have to correct. At best, it’s a 2 line fix, and the fix is already told to you. You just have to ‘type’ it out.

- Adding redundant functionality to already existing code that serves nothing more than the whims of – well, whoever .

- Writing small programs/scripts to do chores that are more company convention than, well,  work.

- Preparing reports or plain old wasting time going through company jargon or conventions that won’t serve ANY purpose outside of the company in particular or in life in general.

- At best, writing code for a piece of software almost entirely designed to the finest detail in the US by the bigwigs – and having little to NO say in it, that will live out its utility in less than 2 months.

Now, consider doing 20 years of doing that, and then one day, looking back at your life and saying – ‘Holy fuck. What DID I do my entire life ? ‘ . You won’t have an answer. Now come back to the present day, and look at the myriad challenges that lie in front of you -

Moore’s law, nanotechnology, space travel, time travel, the origins of the universe, food shortage, population explosion, social issues, national issues, international issue – and these don’t even skim the topmost layer of the top of my head. Imagine devoting your life to the solution of even one of these. ‘What if I fail ?’ – Well, compare that failure to the success of having a workaday, mundane life in which you do effectively ‘nothing’. It’s a choice you have to make.

The choice is yours. I am but a cowherd.

Comments (1)

Follow

Get every new post delivered to your Inbox.