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)
I separate this in two files, like this:
export_excel.py and actions.py
I tried to treat all possible forms of information that may appear in admin, such as properties, functions and normal fields, always getting the column name verbose_name or short_description depending on the case.
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.
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.
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.
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.
Upload an image with file-uploader from http://github.com/valums/file-uploader
and save it to disk with the snippet and also to database if you want to
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.
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
**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.
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.
This decorator is for views that one wants only users of the site to logout based on few conditions
Just add the decorator and it should logout anyuser who doestnot match the condition
btw I borrowed the code from django`s source code