Login

Tag "python"

Snippet List

Python Django CRUD Example Tutorial

Hey Friends, In this quick example, let's see django tutorial & crud example with mysql and bootstrap. step by step explain django crud operations with mysql backend. step by step explain django crud operations with mysql bootstrap. let’s discuss about django crud operations with mysql database. Read more... [https://tuts-station.com/python-django-crud-example-tutorial.html](https://tuts-station.com/python-django-crud-example-tutorial.html)

  • django
  • python
  • crud
Read More

Convert tab indented string to dictionary

output: ``` {u'Ogrenci': [u'Tum okullar', u'Lisans', u'Onlisans', u'Yuksek Lisans / Doktora', u'Ingilizce Hazirlik'], u'Ogretim Elemani': [u'Tum okullar', u'Lisans', u'Onlisans', u'Yuksek Lisans / Doktora', u'Ingilizce Hazirlik']} ```

  • python
  • tab
Read More

Django chunked queryset iterator

The function slices a queryset into smaller querysets containing chunk_size objects and then yield them. It is used to avoid memory error when processing huge queryset, and also database error due to that the database pulls whole table at once. Concurrent database modification wouldn't make some entries repeated or skipped in this process.

  • django
  • python
  • database
  • queryset
  • iterator
  • memoryerror
Read More

Django app/project/directory po messages collector and pofile bulder

Some times if third party application is not translated competelly, developer have to extract and save all of **i18n** strings of third party application to current project, somewhere in root project directory (and configure additional `LOCALE_DIRS` entry) or in any project's applications (usually named "main" or same as project). And after, create **po** file by makemessages, completely translate it and compile it to **mo** file. This script allows you to extract all messages (`msgid`) from all **po** files and save it in **python** file in **django** format — `_("Some i18n message")`, and also compile complex **po** file, contained all **msgids** with related **msgstrs** of third party application. Full example: You have third party app, which is translated to russian, but not completely and names **blog**. It contains two locale dirs and have, for example, the following structure: blog ..locale ....ru ......LC_MESSAGES ........django.mo ........django.po ..contrib ....comments ......locale ........ru ..........LC_MESSAGES ............django.mo ............django.po ......__init__.py ......models.py ..__init__.py ..models.py ..urls.py ..views.py Each po file may contains any set of strings, which may be duplicated accross the application and any other po files, anyway result **po** file will be contain only unique **msgids** and **msgstrs**. This application installed by pip into virtualenv lib directory and of cource you do not want change code here. To translate this application in your project it is possible to copy all (unstanslated) strings to you project as `ugettext` entries (usually `_("Some i18n string")`), make **po** file, translate it and compile **mo** file. So, after call this script (for example, let it be named `pomessages.py`), you will get two files, **django.py** which contains all i18n strings and **django.po** which contains all **msgids** (i18n strings) with related translations. `pomessages.py path/to/third-party-app/root locale [project-name:default is empty] [filename:default=django]` In this case: `pomessages.py path/to/blog ru blog django` After script will be finished, in blog directory will be added two files, **django.po** and **django.mo**. **django.py** content: ... # blog:admin (source:ru) directory translations # ---------------------------------------------- _("Category") # blog:models.py:10, blog:views.py:15 _("Article") # blog:models.py:20 # blog:category/admin (source:ru) directory translations # ------------------------------------------------------- _("Add Category") # blog:category/admin/templates/admin/create.html:10 _("Edit Category") # blog:category/admin/templates/admin/change.html:20 ... **django.mo** content: ... # msgid "" msgstr "" "Project-Id-Version: Blog\n" "POT-Creation-Date: 2016-02-08 12:00+0000\n" "PO-Revision-Date: 2016-02-08 18:00+0000\n" #: path/to/file.py:10 msgid "Category" msgstr "Категория" #: path/to/file.py:20 msgid "Article" msgstr "" ... After you just need to place **py** file anywhere in your project and place **po** file to the following locale directory (or merge with existing **po** file if it exists), run: `django-admin makemessages -lru` to fix **po file** (remake it), translate missing entries and run: `django-admin compilemessages`, reload project and you will have translated third party application.

  • django
  • i18n
  • python
  • pofile
Read More

Trim the center of a string

I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly. So i wrote this little function to cut the center of a string i thought it cute so i leave it here. Pay attention to the comment so you can see what is going on.

  • python
  • string
  • slice
Read More

Generate a dineromail form in python

This is the code we use on bandtastic.me to build a html that sends users to dineromail to pay with. This code builds a form thats ready to send multiple items.

  • django
  • python
  • dineromail
  • express checkout
Read More

Manual CSRF check for Django Facebook canvas applications

The way to manually control CSRF correctness for FB applications. Automatic check cannot be used because FB does POST on your canvas URL when initializing your application without CSRF token. If you still want to use Django CSRF stuff do manual checks. You only need to perform manual check when there is no correct signed_request present in your request - correct request means you really deal with FB. Use facebook_csrf_check to verify POST requests when signed_request is absent.

  • django
  • python
  • post
  • facebook
  • csrf
  • fb
Read More

Django Dictionary Model

This is a model that implements (most of) the python dictionary interface. Meaning, you can work with this model exactly like a python dictionary, and it handles querying the database for it's values, saving/deleting the helper objects, etc. I wrote this originally when I needed to store an arbitrary dictionary in the database, and decided to work it up into a near-complete implementation of a dictionary. In order to make sure that the dictionary is the most optimized possible, I have a static method that can be used for retrieval. Feel free to ignore it if you don't care about optimizing database queries. Here's an example: Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from binder.models import Dictionary >>> d = Dictionary.getDict('Foobar') >>> print d {u'Foobar': u'omgbbq', u'HAHAHAH': u"who's afraid of a big, black, bat?"} >>> d['pot'] = 'The kettle is black.' >>> print d {u'Foobar': u'omgbbq', u'pot': u'The kettle is black.', u'HAHAHAH': u"who's afraid of a big, black, bat?"} >>> print d['pot'] The kettle is black. >>> for k, v in d.iteritems(): ... print k +":", v ... Foobar: omgbbq HAHAHAH: who's afraid of a big, black, bat? pot: The kettle is black. >>> print d.keys() [u'Foobar', u'HAHAHAH', u'pot'] >>> print d.values() [u'omgbbq', u"who's afraid of a big, black, bat?", u'The kettle is black.'] >>> There's several more functions that I've implemented; check the code to see. (An interesting note: DictField saves immediately upon making a change, which is good to keep in mind in case that functionality isn't expected.) Hope someone finds this useful. :) --Chris

  • model
  • python
  • dict
  • dictionary
Read More

Expose filtered settings to templates request context

**Warning**: I'm quite sure this is **not** a best practice, but this snippet has proven being very useful to me. Handle with care. I also wonder about the impact on performance, while I didn't notice any slowdown on a very small app of mine. Idea is to expose project settings to template request context through a context processor, and \__doc__ should be self-explanatory. Of course, if you know a better way to achieve the purpose, please tell in the comments.

  • templates
  • settings
  • python
  • context_processor
Read More

JavaScript implementation of Python xrange() builtin

I don't like not having the `range()/xrange()` in JavaScript — particularly when working with [Underscore.js](http://documentcloud.github.com/underscore/) and other such libraries — so I wrote it. It's not rocket science, but it might help make the world a slightly less annoying place for a couple of people.

  • javascript
  • python
  • list
  • generator
  • array
  • builtin
  • xrange
Read More

54 snippets posted so far.