Login

Most bookmarked snippets

Snippet List

Create OpenOffice documents

This view will enable you to generate OpenOffice documents from templates written in OpenOffice 2.x Just make sure that there is no OO tag in between your code (no change in formatting etc.). content.xml is a valid XML file, so you can do some preprocessing using xml.dom.minidom. I would also recommend caching (just save the zip file without content.xml and content.xml on its own).

  • print
  • odf
  • office
Read More

get_object_or_none

This utility is useful when you want to safely retrieve a single object from the database without having to wrap get() in a try/except block every time. It also supports query optimization via select_related and prefetch_related, making it ideal for performance-conscious applications.

  • orm
  • queryset
  • utils
  • select_related
  • get_or_none
  • prefetch_related
Read More

Browser-native date input field

Most modern browsers support the new `<input type="date">`, which allows inputting date using a browser-native date picker with built-in validation. This form widget utilizes this feature by setting the input type to "date" and by changing the value formatting as it should be in the ISO format. See more about `<input type="date">`: <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date>

  • forms
  • datefield
  • widget
  • input
Read More

A wrapper around cache_page making it optional

Make `cache_page` optional, depending on the result of a callable. Uncomment the added lines if you want to make sure that the consumers don't know the page is cached – that means more hits on your end, but also a guarantee that they will always get the newest data asap.

  • cache
  • caching
  • cache_page
Read More

Closest ORM models to a latitude/longitude point

Here's how to find the 10 closest locations to a point for a model that has latitude and longitude columns, if you're not using GeoDjango. This runs a brute force distance calculation against every row, so it should only be used on smaller tables - probably less than 100,000 rows. For larger tables you should use GeoDjango instead. See also [my TIL](https://til.simonwillison.net/postgresql/closest-locations-to-a-point) about this.

  • orm
  • gis
  • latitude
  • longitude
  • location
Read More

Strict Boolean Form Field

A form field for a Boolean that forces the user to make a choice from a list of choices. **Use Case** You have a Yes/No question the user must answer, but they may answer it yes or no. You don't want to supply a default because your need to force the user to actively select their answer. If they do not select an answer, the field should raise a validation error, like "This field is required". Normal BooleanField logic is based on a "checkbox", which, when "required" is required to be checked. This logic assumes that an empty value is the same as False -- in fact, there is no way for validators to distinguish between the empty value and False. Based on excellent suggestion from Peter DeGlopper: https://stackoverflow.com/a/56677670/1993525

  • form_field
Read More

Calculating Maintainability Index for a whole project

With this command you can calculate the maintainability index for your whole project. In your settings you have to add a dictionary called `RADON_MI_SETTINGS`. It could be like this: ```python RADON_MI_SETTINGS = { 'paths': ['projectname'], 'exclude': 'projectname/some_app/some_file.py', 'ignore': 'migrations,tests', } ``` I had to add following packages: ``` radon==3.0.1 progress==1.5 plotly==3.7.0 GitPython==2.1.11 ``` Following commands are available: * `python manage.py calculate_maintainability_index` Only display the maintainability index of the project. The average from every file is build by using the logical lines of code per file. * `python manage.py calculate_maintainability_index --init` Go through every commit filtered by their commit_message (is set to “bump version” currently) and calculate the maintainability index for the whole project. This creates a file with the history. * `python manage.py calculate_maintainability_index --showhistory` Display the history of the maintainability_index in a graph in your browser. * `python manage.py calculate_maintainability_index --commit` Calculate the current maintainability_index and append it to your history. Commit your edited history file. * `python manage.py calculate_maintainability_index --fail` Calculate the current maintainability_index and raise an Error, if it is lower than the last entry in your history. Useful for use in an automated pipeline. Hints: * radon has a problem with large lists and dictionaries. If you have a file with a list or dictionary with more than 100 entries, you should exclude it. * To initialize your history you should change the commitmessage filter to something, that suits your needs. Created by Martin Becker at [Jonas und der Wolf GmbH](https://www.jonasundderwolf.de)

  • django
  • command
  • git
  • maintainability
Read More

Bootstrap Accordian

<p>This is an example of Leamug web development, you can find how to add <a href="https://starttemplates.com/product/bootstrap-accordion/">start templates designer</a></p> Collapsible accordion are useful when you want to hide and show large amount of content. If you are looking for a modern, flat and professionally designed Bootstrap accordion, then we have a best choice for you called Trendy css build collapse accordion with plus and minus icons which can be used in your all types of websites for <a href="https://hindilife.in/majedar-masti-bhare-funny-chutkule-hindi-mein/">Latest Majedar Jokes</a> The Bootstrap accordion with four panels is created. The main div is assigned the panel-group class. The role data attribute is set as tab list while aria-multivariable is set as true. The panel with content div is assigned the panel and panel-default classes while its inner div contains the classes for panel heading, title, and content of the panel. The div showing the panel content is given the panel-body class along with collapse and panel-collapse classes. It will make the panel body collapsed as web page loads.

  • Responsive
  • Bootstrap
  • Web design
Read More

Support disabling options in a select widget

Provide the ability to disable a select option with Django2. This can be done during the widget initialization (i.e when the widget field is created) or during form initialization. This will disable the select option based on a context or by specifying the values that should be disabled.

  • widgets
  • django-forms
Read More

3110 snippets posted so far.