background preloader

Web Design Video and Development Tutorial Library // Think Vitamin Membership

Web Design Video and Development Tutorial Library // Think Vitamin Membership

Custom template tags and filters Django’s template system comes with a wide variety of built-in tags and filters designed to address the presentation logic needs of your application. Nevertheless, you may find yourself needing functionality that is not covered by the core set of template primitives. You can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load %} tag. Code layout Custom template tags and filters must live inside a Django app. The app should contain a templatetags directory, at the same level as models.py, views.py, etc. Your custom tags and filters will live in a module inside the templatetags directory. For example, if your custom tags/filters are in a file called poll_extras.py, your app layout might look like this: polls/ models.py templatetags/ __init__.py poll_extras.py views.py And in your template you would use the following: There's no limit on how many modules you put in the templatetags package.

Outputting PDFs with Django This document explains how to output PDF files dynamically using Django views. This is made possible by the excellent, open-source ReportLab Python PDF library. The advantage of generating PDF files dynamically is that you can create customized PDFs for different purposes – say, for different users or different pieces of content. For example, Django was used at kusports.com to generate customized, printer-friendly NCAA tournament brackets, as PDF files, for people participating in a March Madness contest. Install ReportLab Download and install the ReportLab library from Test your installation by importing it in the Python interactive interpreter: If that command doesn't raise any errors, the installation worked. Write your view The key to generating PDFs dynamically with Django is that the ReportLab API acts on file-like objects, and Django's HttpResponse objects are file-like objects. Here's a "Hello World" example: Complex PDFs Other formats

Gmail and Django - ltslashgt Did a bit of running around today to get Django sending email via Gmail. It’s simple once you figure it out. If you’re running 0.96, upgrade to the latest development version or apply the patch from ticket #2897. 0.96 does not support TLS, which Gmail requires. Then add the appropriate values to settings.py: EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = 'youremail@gmail.com' EMAIL_HOST_PASSWORD = 'yourpassword' EMAIL_PORT = 587 You can use the shell to test it: >>> from django.core.mail import send_mail >>> send_mail('Test', 'This is a test', to=['youremail@somewhere.com']) Edit: Bryan commented that send_mail is deprecated. >>> from django.core.mail import EmailMessage >>> email = EmailMessage('Hello', 'World', to=['youremail@somewhere.com']) >>> email.send()

Configuring other mail clients - Gmail Help Google Apps-gebruikers moeten de standaardinstructies volgen tenzij anders aangegeven, waarbij 'jouw_domein.nl' moet worden vervangen door de feitelijke domeinnaam. Door veel clients worden de geschikte IMAP-instellingen voor je account automatisch geconfigureerd, maar bevestig dat de instellingen voor de verbinding die door de client worden geconfigureerd dezelfde zijn als degenen die hieronder worden vermeld. Als je een client gebruikt die hieronder niet wordt vermeld, kun je de volgende informatie gebruiken om IMAP te configureren. Als je problemen hebt, neem je contact op met de afdeling klantenservice van je e-mailclient voor verdere instructies. Server voor inkomende berichten (IMAP); hiervoor is SSL vereist imap.gmail.com Poort: 993 SSL vereist? Als je client geen SMTP-verificatie ondersteunt, kun je met deze client geen e-mail verzenden vanaf je Gmail-adres.

PDF Toolkit documentation — reportlab v2.4 documentation Navigation Next topic package reportlab.pdfgen This Page Show Source Quick search Enter search terms or a module, class or function name. ReportLab PDF Toolkit documentation¶ API references and more for the ReportLab PDF Toolkit Contents: Sphinx is being used for the automated API references. © Copyright 2010, Robinson, Becker, Watters and many more. A Detailed Django Tutorial: Blog Basics Part IV This is the last part of this 4 part series by Jeff Hui. If you wish, you can download the entire tutorial without having it split up. Today we’ll be learning the gist of Django, a pythonic web framework. In the spirit of a stereotypical web framework tutorial, we’ll produce a primitive blog with the minimal amount of code possible. Yellow background color text indicates text to be entered into a command line/terminal and blue indicates code. If you’ve followed my django screencast, you’ll be familiar with the most of what this tutorial covers. If you’ve never dealt with (or even seen) python code before, I recommend skimming through python’s official tutorial. if myvariable == True: print “myvariable is True” print “Always printed” One other thing to note is that boolean variables are True and False (case-sensitive). Table of Contents Comments What’s a blog without comments? ‘django.contrib.comments’, Then we need to map it to a url. Now we can start adding comments to our posts. Homepage

Model instance reference A few object methods have special purposes. __unicode__ Model.__unicode__() The __unicode__() method is called whenever you call unicode() on an object. For example: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) def __unicode__(self): return '%s %s' % (self.first_name, self.last_name) If you define a __unicode__() method on your model and not a __str__() method, Django will automatically provide you with a __str__() that calls __unicode__() and then converts the result correctly to a UTF-8 encoded string object. __str__ Model. The __str__() method is called whenever you call str() on an object. from django.db import models class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) def __str__(self): return '%s %s' % (self.first_name, self.last_name) The previous __unicode__() example could be similarly written using __str__() like this: <!

Sending e-mail Although Python makes sending e-mail relatively easy via the smtplib library, Django provides a couple of light wrappers over it. These wrappers are provided to make sending e-mail extra quick, to make it easy to test e-mail sending during development, and to provide support for platforms that can’t use SMTP. The code lives in the django.core.mail module. Quick example In two lines: from django.core.mail import send_mail send_mail('Subject here', 'Here is the message Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. Note The character set of e-mail sent with django.core.mail will be set to the value of your DEFAULT_CHARSET setting. send_mail() send_mail(subjectmessagefrom_emailrecipient_listfail_silently=Falseauth_user=Noneauth_password=Noneconnection=None) The simplest way to send e-mail is using django.core.mail.send_mail(). The subject, message, from_email and recipient_list parameters are required. send_mass_mail() mail_admins() mail_managers()

Related: