Caching data (image, rss) with django - django

That's my first question in here, I've been looking through old questions, but nothing matched with my problem. Here it is.
I'm creating some site with one main functionality. We want this site to display content of other sites, but in a specific way. User chooses let's say two pages from five and want to see their content. He clicks button 'Display' and goes to next page where he finds let's say view from web cam, and here comes problem.
I want to cache image that is hidden behind the url from which image was downloaded, so after refresh image won't be downloaded again, but browser will get it from cache.
I've been looking through documentation of Django, but nothing seemed to be useful.
I know that I should:
1) create table which stores cache
2) add to settings.py some CACHE_BACKEND = ...
3) use #cache_page(300) before declaration of function which returns content which should be cached,
but... it doesn't seem to work.
I will be greateful if someone tells how to solve that problem, maybe with some sort of code showing the mechanism.
Cheers,
Chris.

I think that right way to do this will be to store image somewhere on your server and delete it later with cron or something similar.
Django cache framework wasn't created for the purpose you are trying to use it.

Related

Is there an equivalent of 'list_editable' for FileFields in django?

I'm making a website with django where you can basically upload a file to the django admin and download it whenever you need to view it. Very simple (making it for my dad which wants to organize his excel files for his work).
This is the problem, I hard coded the download path to the file I uploaded, which makes me have to modify the path every time I upload a new file with the same name (since django adds a random string of digits and characters every time you upload a file with the same name as before). What i'm currently doing is really inefficient, and would like to change that as soon as possible.
My idea is to make the FileField name editable in the django admin. I've come across this:
list_editable = ['asd'].
I thought that might work and so I tried it. The results were interesting. I immediately saw a 'Save' button on the bottom of the admin site, which wasn't there before. But I still couldn't edit the FileField. I've searched this on multiple forums, the django documentation and more forums, only to not find anything useful.
This is a picture of how the admin page looks when I added the list_editable to my FileField.
So, I decided to look for an equivalent to see if that works. Again, I searched on multiple forums, but still found nothing.
Any type of answer or recommendation is very much appreciated.
Thanks in advance!!

Django strategy for automatically suggesting matching content

I'm looking at porting a custom-written PHP CMS into Django. One of the features the CMS currently has is an image upload function. I write an article, tag it with information, then choose a photo for it. If the system has any photos which have been added to articles with tags in common with the new one, it will suggest the photo for that article too. If there are no matches then a new image can be added.
In case this doesn't make sense, let's say I tag an article as Bruce Springsteen, The Beatles and Led Zeppelin. Next time I add an article with the tag The Beatles, it should suggest I use the image added for the first article.
What would be the best Django-applicable way to implement this? I've looked at the Photologue app and have integrated it, and I know it has tagging support (the problem here is that I'm using django-taggit, whereas Photologue supports django-tagging). One approach could be simply building it myself -- when a user uploads an article, I run a hook after they save it to associate the tags with the image. I'm just not sure how to then autosuggest an image in the admin tools based on that info.
Any ideas/approaches greatly appreciated.
This is almost certainly something you're going to have to build yourself. Django has a moderate number of libraries out there (that you've clearly already found). Unlike other solutions, it doesn't have a lot of things that get you 100% to your desired solution (whereas something like Drupal might get you 100% of the way there).
What you will probably need to do (at a high level) is something like this:
Create an AJAX view that takes the current tags as an argument and does a query on the existing posts to see what tags match and returns images from those posts.
Use jQuery/javascript on your view to call your AJAX view on the page as tags are added
Use jQuery to update a <div> on your page and show all the images that your view returned
Here is a similar example that might help get you started.
You might look into django-ajax as a helper library for your requests, but it definitely isn't necessary.
The hook between the your image module and any other django module can be implemented using django's contenttypes framework which also provides some useful instance methods for returning related/hooked objects.

Storing Django translations in a database

I am working on a Django application which is going to be deployed to Heroku (on their Cedar stack). A limitation of this is that we cannot write to disk. However, Django translations reside in .po files, and therefore the client will be unable to change the translations on the live site without involving a developer (which is not ideal).
The alternative solution therefore seems to be to store the translations in either an RDBMS, Mongo, Redis etc etc.
Is there any sensible way of achieving this? Is it even a good idea? (I wouldn't want to hit the DB for every translation!)
Edit: There seem to be lots of Django apps out there for translating text which is stored in a DB, but not for actually storing the translations themselves in a DB.
What I think about Django translations and translations stored in the files in general - this is for the parts which will not change and is not dynamic. Like constants you have in the website. And if you have dynamic text, which could be and must be edited when the website is running - I would say this is the same thing as normal content (blog entry, comments and so on).
So you can just develop simple module "site parts", with the template tag, which will grab the right thing for you from DB. Like from template you can call { get_site_part example }. And then you can edit those parts from admin interface.
I would not recommend to store dynamic content to the system files. If you worry about hitting database every time you need this - then caching should help here also you can develop smart template tag and grab all the site parts you need in one query. Then it will be nothing different from just simply loading blog entry or the comment for it.
Maybe I'm wrong, so this is just my 2c on this topic :)
Ignas

Facebook Open Graph OG: Meta Tags - Works only sometimes?

I have a like button on my site, i have defined all the og: meta tags and its works for most of most pages, but doesnt for 2 other pages. Its using a template so its exactly the same code, how can it work for some but not all pages?
For the pages it doesnt work, it doesnt pick up the title, image, link or description, basically any of the meta tags information.
Working like button....
http://www.imoffonholiday.com/holiday.php?id=des_home&destination=faliraki
Not working Like button
http://www.imoffonholiday.com/holiday.php?id=des_home&destination=ayianapa
Any ideas?
One usual problem is caching. If you were testing and at some point had the wrong data in the metatags facebook will cache that info.
One simple wat to test if it is a caching issue is to add a random param at the end of the URL.
so www.yourpage.com/index.php?cacheBust=1
Running the Facebook Debugger often clears those kind of things up. It appears to refresh the FB cache when you do.

Facebook Like Button Cache. Linter useless

Looks like an issue was closed related to the question I'm about to ask, so I wonder if I'm going to be skating on thin ice, but here goes. I feel like even though this is kind of localized by nature, it could be a useful example for other developers dealing with the Like button.
I seem to be having issues regarding the Facebook like button. The infuriating part is that I'm pretty confident I have everything setup properly, and even though the linter says "hey, this all looks kosher!" the like button fails to get the correct content and uses cached info from a different page.
Here's the case: referlocal.com. We serve daily deals, and list them right off the homepage. There can be one of many deals listed on that homepage, so obviously Facebook is seeing just one when it goes to take a peek at the root directory. Now, on the homepage, whatever deal your viewing has a FB Like XFBML tag with href and ref attributes set. Every page that displays a deal also has OG tags pointing directly to the offer view page. Deals can also be viewed on user pages. So, on these three paths:
/
/{username}
/offers/{city}/{title-url-alias}
a like button is included as well as OG tags that point to /offers/{city}/{title-url-alias}. For about 3 weeks, the button worked like a champ. But recently, it's been performing strangely. For any deal, regardless of the 3 locations, it always uses the information cached from the previous days "/" deal. Regardless of the OG tags or origination.
Here's the wildly confusing part. The Facebook Linter is supposed to recache the information on the page, right? Well, it sort of does. It picks up on all of the appropriate information set in the OG tags, but when you click the Like button at the bottom of the page, no dice, still uses the deal FB saw on the homepage from the day before.
I know this is probably killing my argument for localization, but check this out:
http://developers.facebook.com/tools/lint/?url=http%3A%2F%2Freferlocal.com%2Foffers%2Fdallas%2Fget-20-of-authentic-italian-american-cuisine-for-only-10-at-leggios-italian-ristorante-dallas
Regardless of the fact that the linter finds all the appropriate information, the like button at the bottom still is liking a deal FB cached from the previous day.
Any ideas?
I had this problem too. The linter caches things in a very weird manner - seems like the best thing to do is either to add a query string and change it every time, or just rename the file every time you're checking.