⭑ posts tagged with 'django'

Useful Web Dev Links Mega-Post

I will attempt to keep adding all of the useful links I find to this post rather than have them scattered across various notes and tabs.

Layout and components

every-layout.dev
component.gallery
csslayout.io – patterns
every-layout.dev – css components
web.dev – building a sidenav component
css-tricks.com – styling comment threads
web.dev – lazy loading images
web.dev – building a multi-select component
height.app – context menus
css-tricks.com – add to calendar button
ishadeed.com – position sticky css grid (admin layout)
ishadeed.com – building real-life components
dev.to – css only components
freecodecamp.org – recreate Medium's article layout with css grid
freecodecamp.org – back-to-top button and page progress-bar
smashingmagazine.com – complex web tables
css-tricks.com – perfect table of contents
adamsilver.io – where to put buttons on forms
uxdesign.cc – designing text fields
matuzo.at – html boilerplate
codeburst.io – how to create horizontal scrolling containers
web.dev – common UX patterns
benmyers.dev – description lists
smashingmagazine.com – complete guide to accessible front-end components

CSS

simplecss.org
smolcss.dev
moderncss.dev
bansal.io – pattern css
patterns.helloyes.dev maintainablecss.com
simonhearne.com – preventing layout shifts with webfonts
daily-dev-tips.com – css attribute selectors
speckyboy.com – creative hyperlinks
egghead.io – dark mode with css variables, calc and hsl
csswizardry.com – writing tidy code
csswizardry.com – your logo is an image not an h1
fvsch.com – base stylesheet
moderncss.dev – custom select styles
moderncss.dev – popular website heroes created with css grid
css-tricks.com – your css reset needs text size adjust
css-tricks.com – block links
css-tricks.com – consistent fluidly scaling type and spacing
joshwcomeau.com – full bleed
ishadeed.com – aligning content with different wrappers
ishadeed.com – tiny enhancements in css
ishadeed.com – defensive css/
ishadeed.com – fit-content
ishadeed.com – conditional border radius
fluid-typography.netlify.app
calibreapp.com – css performance

CSS Resets

joshwcomeau.com – custom css reset

Buttons

getcssscan.com – css button examples
css-tricks.com – a complete guide to links and buttons
web.dev – building a split button component
fvsch.com – styling buttons

Gradients

larsenwork.com – better gradients (easing gradients)
css-houdini.rocks – corners gradient
joshwcomeau.com – beautiful gradients

Shadows

joshwcomeau.com – designing shadows
joshwcomeau.com – shadow palette
getcssscan.com – css box shadow examples

Icons

iconic.app lucide.dev iconoir.com
remixicon.com
heroicons.com
tablericons.com
feathericons.com
icons.radix-ui.com
phosphoricons.com

Fonts

fontpair.co

Javascript

gomakethings.com – automatically expand textarea as the user types

Django

snyk.io – Django security tips
openfolder.sh – Django speed tutorial
rockandnull.com – custom user model
engineertodeveloper.com – dynamic formsets
spookylukey.github.io – Django views the right way
valentinog.com – how to handle multiple Django sites
mattsegal.dev – how to make your project easy to move
untangled.dev – retrieving related data in Django
chris-lamb.co.uk – GeoDjango and the UK postcode database
openfolder.sh – keeping logic out of templates and views
levelup.gitconnected.com – optimising Django queries
pythoncircle.com – adding an email subscription feature
stackoverflow.com – how to automatically login a user after registration
mattlayman.com – secure apps
medium.com – performance problems in the Django ORM
rajasimon.io – Django Channels for real-time updates
levelup.gitconnected.com – What I learned from building a Django appointment scheduler for my school
djangodoctor.medium.com – CharField or TextField

Working with APIs

dev.to – how to populate your database from an external API
medium.com – how to seed a Django API with data from an external API

Django general advice and tips

rajasimon – Django project structure
brntn – six things I do every time I start a Django project
reddit – multi-tenancy discussion

Django project tutorials

engineertodeveloper – how to build a photo gallery
dev.to – how to build an email newsletter subscription service
freedjango.com – generate a QR code

Django uploading files

djangocentral.com – uploading images
soshace.com – upload multiple images
dev.to – upload multiple images simultaneously using dropzone.js

Database design

simeongriggs.dev – designing a more complete recipe website

HTMX

htmx.org – file upload/
github.com/spookylukey/django-htmx-patterns
blog.benoitblanchon.fr – modal form
justdjango.com – dynamic forms
andytwoods.com – modal popup
rockandnull.com – freedom for html input elements using htmx
saaspegasus.com – htmx and alpine.js
betterprogramming.pub – full page reloads with django and htmx

Email

mattlayman.com – testing email designs

Examples and inspiration

dannpetty.com/

Misc

ubiq.co – preventing image hotlinking with nginx
marcysutton.com – links vs buttons in modern web applications
uigoodies.com
copybook.me
technology.blog.gov.uk – why the gov.uk design system team changed the input type for numbers
dev.to – productive tools for web development
blog.datawrapper.de – which colour scale to use in data vis
devhints.io
pexels.com – nice photos
logoipsum.com – placeholder logos
colorpalettes.colorion.co – colour palettes
ahrefs.com – meta tags

Updated on 10 Nov 2022

HTMX - Create responsive web apps with ease

HTMX is a new library that allows you to create modern responsive web apps without writing loads of custom javascript or using a bulky frontend framework like Vue or React.

From the HTMX website:

HTMX gives you access to AJAX, CSS Transitions, WebSockets and Server Sent Events directly in HTML, using attributes, so you can build modern user interfaces with the simplicity and power of hypertext.

Here's a good series of videos from BugBytes on YouTube which shows how to integrate HTMX into your Django project and all of the great things you can do with it: https://youtube.com/playlist?list=PL-2EBeDYMIbRByZ8GXhcnQSuv2dog4JxY

Custom 404 error pages in Django

It's pretty straightforward to add a custom 404 page to your Django site.

Create a 404.html template in your templates folder. It can be named whatever you want, just make sure you use the same name in your view.

Add the following to your views.py file:

def entry_not_found(
  request,
  exception,
  template_name='404.html'
):
  return render(request, template_name)

Then in urls.py add the following outside of the urlpatterns list:

handler404 = 'your_app.views.entry_not_found'

Remember to set DEBUG = False in your settings to see the results.

How to use the built-in HTML datetime-local widget in a Django form

I was looking for a simple datetime picker for my blog post form, assuming I'd have to use a third-party javascript thing, but as it turns out there's one built into HTML and it has broad browser support. Great!

You can specify you want to use the datetime-local widget very simply:

dt_field = forms.CharField(
  widget=forms.TextInput(
    attrs={ 'type': 'datetime-local' }
  )
)

But it doesn't play very nicely with Django forms. The default behaviour probably works fine for many 'create' views, but it doesn't work if you have to specify an initial value or you want to edit a datetime value. The datetime from Django is in the wrong format to be rendered by the widget so it just shows as blank, meaning the user has to set it every time they edit the form – not ideal.

Thankfully I found a very nice solution in this Stack Overflow comment. Simply define a custom datetime-local input and field type (I put this at the top of the forms.py file):

from django import forms

class DateTimeLocalInput(forms.DateTimeInput):
  input_type = "datetime-local"

class DateTimeLocalField(forms.DateTimeField):
  # Set DATETIME_INPUT_FORMATS here because, if USE_L10N 
  # is True, the locale-dictated format will be applied 
  # instead of settings.DATETIME_INPUT_FORMATS.

  input_formats = [
    "%Y-%m-%dT%H:%M:%S", 
    "%Y-%m-%dT%H:%M:%S.%f", 
    "%Y-%m-%dT%H:%M"
  ]
  widget = DateTimeLocalInput(format="%Y-%m-%dT%H:%M")

Then use the newly defined field in your form:

class Form(forms.Form):
  dt_field = DateTimeLocalField()

And that's it! In your form you will get a nice looking datetime-local picker with no third-party javascript or plugins required.

Building the Blog v1.0

I've been saying I would start a blog for years but when the pandemic hit in 2020, and I suddenly had a lot of free time, I thought now is the time to finally tackle this thing. Build a simple blog, get it online, and start writing. I made plans and even a few prototypes, but then just... stopped...

If I'm honest, I didn't really do anything for most of the rest of 2020 apart from work and watch King of the Hill, but at the tail end of 2021 I picked it back up and made a commitment to finally get it done.

I wanted to keep things as simple and lightweight as possible so the public facing blog is built with just Django / html / css. I also made a custom admin site with a little javascript sprinkled in to improve the user experience in some places (modals, forms).

Here's a quick rundown of what I used:-

I have lots of plans to improve things but I'm happy with how it turned out.

Django Taggit - Removing Unused Tags

Here's a useful code snippet that removes unused django-taggit tags.

 from taggit.models import Tag

 def remove_all_tags_without_objects():
  for tag in Tag.objects.all():
    if tag.taggit_taggeditem_items.count() == 0:
      print('Removing: {}'.format(tag))
        tag.delete()
    else:
      print('Keeping: {}'.format(tag))

The code is from Paul Mestemaker on Github: https://github.com/jazzband/django-taggit/issues/294

I needed to quickly fix a problem on my development server that had resulted in lots of unwanted tags so I just put the code in an existing view, loaded it once, checked the results, then removed it.

If you wanted to have the option of reusing it you could put it in a custom admin command as suggested in the Github issue thread.