Login

Most bookmarked snippets

Snippet List

Whore links

Finds links from a body of html so you can display them elsewhere.

  • beautiful-soup
  • links
Read More

Another pygments for ReST

This is like [snippet 36](/snippets/36/). And it'll return css also. And it's not a filter.If the code parameter is skip, it'll test the code first, and if there is not a suiable lexer for the code, then use default python lexer to render the code. The code lanauage parameter is comes from pygments lexer alias. How to use it ---------------- html = to_html(rest_text) And there is a level parameter in to_html function, default is `2`, it's the sections level. And the html will be `css style + body` How to write ReST --------------------- Below is a example. This is a test. .. code:: def code(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): opt = {'display':'on'} opt.update(options) docnodes.Node(content, ''.join(arguments), **opt) if opt['display'].lower() == 'on': return [nodes.literal_block('', '\n'.join(content))] else: return [] .. code:: html+django <h1 id="title">通讯录</h1> <hr> <div> <table border="0" width="500"> <tr align="right"> <td>{% if has_previous %} <a href="/address?page={{ previous }}">上一页</a> {% endif %} {% if has_next %} <a href="/address?page={{ next }}">下一页</a> {% endif %}</td></tr> </table>

  • pygments
  • rest
Read More

slugify js -> python

This code is derived from the slugify JS function used in django's admin interface. It will create django-compatible slugs for you. Sometimes I do batch imports and need my items to have slugs, I give this script the item's title and get a slug back. Simple

  • slugs
Read More

convert plain text to html

Convert plain text to html. For example: text="""aabhttp://www.example.com http://www.example.com http://www.example.com <<< [<aaaa></aaaa>] """ print plaintext2html(text) It can convert url text to html href. And it also can convert space to &nbsp;. So if you paste python code, the indent will not lost at all. Default it'll convert \t to 4 spaces.

  • text
Read More

Month / Year SelectDateWidget based on django SelectDateWidget

A more simple version of [https://djangosnippets.org/snippets/1688/](https://djangosnippets.org/snippets/1688/), inheriting from `SelectDateWidget`, overriding only the necessarily. **Usage example:** **In models.py:** from django.db import models from django.utils.translation import gettext_lazy as _ class MyModel(models.Model): start = models.DateField( _("Start date"), ) end = models.DateField( _("End date"), ) class Meta: verbose_name = _("My model") **In forms.py:** from django import forms from .models import MyModel from .widgets import MonthYearWidget class MyModelForm(forms.ModelForm): class Meta: model = MyModel exclude = [] widgets = { "start": MonthYearWidget(), "end": MonthYearWidget(last_day=True), } **kwargs:** - *last_day :* if set to `True`, returns the last day of the month in the generated date.

  • widgets
  • select
  • year
  • month
  • SelectDateWidget
Read More

Verify events sent to your webhook endpoints

Third party services (e.g. Stripe) optionally sign webhook events to verify it is them sending events. If the third party you use does not provide an SDK or official library for verifying signatures, you can manually verify the signature with this snippet.

  • signature
  • third-party
  • webhook
  • hmac
Read More

Django Standard API Response Middleware for DRF

As you can see, if you using django-rest-framework, you will found many different response format. This middleware to solve all of these problems with Standard API Response. All HTTP Response status stored into json response, not in HTTP Status (because mobile application, like android can't fetch the response body when HTTP Status >= 400).

  • middleware
  • django
  • api
  • django-rest-framework
Read More

a better Django Slug Unique Generator

let say the user chooses the name "Elsa Frozen" now his slug would be "Elsa-Frozen-5" it means 4 other people have used the same header now he can go to url: "your website.com/Elsa-Frozen-4" to see other people's Post

  • django
  • slug
  • url
Read More

CBV to mix FormView and DetailView functionalities

Use this to display an object on a page AND be able to process a form on this page. This is just a copy of the [Django docs advice](https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/#using-formmixin-with-detailview), but put as a reusable, standalone View.

  • view
  • form
  • cbv
  • detail
Read More

3110 snippets posted so far.