Login

Snippets by sansmojo

Snippet List

Additional Change List Columns

Ever wanted to add a link or two at the end of a row in a model's change list? Say you had a model for people and a model for registrations or blog posts and you want to modify the people change list so it has a link to, say all of the registrations or blog posts for the person. Well, Django provides half of the solution already in that the example registration change_list already handles the display of all registrations tied to that person. For example, the url `/admin/registrations/registration/?person__id__exact=121` gets you to a filtered list of registrations for the person with the id of 121. This is the same url used if you use list_filter in your model definition (though setting list_filter is not required for what we're doing). Okay, so to add a link to the end of each person row in the change list, you need to create a template tag similar to "person_result_list" in the code. There, I've given an example that adds two links. Each dictionary in additional_links needs to have at least a text, sortable, and url_template attribute. The text attribute is what will display as the header to the column. url_template will be fed the id of the row object (in this example, a person id), which you can use to construct the link for each row. You could extend this if you wish, though all I ever need is the id. And the last step is to use your new template tag in a modified change_list.html in place of the default result_list tag. The example at the bottom of the code shows an example usage of the tag. Hope this makes sense and is helpful to someone!

  • template
  • admin
  • tags
  • change_list
Read More

email_links

A very basic app centered around a template tag that takes an e-mail address and optional label and returns a link to an e-mail form. This way, you can easily throw e-mail addresses into your template that will automatically link to an e-mail form instead of displaying the e-mail address itself. The model for this app is extremely simple, with only one field to store the e-mail address. When an e-mail is passed to the email_link template tag, the e-mail address is added to the database if it is not already there. This is stored in the database in order to keep the full e-mail address hidden from the viewer. To use, simply create an app called 'email_links' (or you can change the name if you also change line 4 of email_extras.py to reflect the change). Then use the models.py and views.py provided. Add a templatetags directory under your app, and add email_extras.py and a blank __init__.py In your urls.py, add the following to your urlpatterns (subsitute project_name for your own): `(r'^email/(?P<email_id>[0-9]+)/$', 'project_name.email_links.views.email_form'),` Create two templates: email_links/email_form.html and email_links/thanks.html (the examples here are extremely basic - make them however you want). Finally, in a template where you want to provide a link to e-mail someone, first load email_extras: `{% load email_extras %}` Then do one of the following: `{% email_link "[email protected]" %}` `{% email_link "[email protected]" "E-mail someone" %}` Both will create a link to the e-mail form. The first will use mask_email (from the snippet by jkocherhans) to create the link. The second will display the second argument "E-mail someone" in the link. Also note that I've used the Sites add-on to generate the links, but you can easily change this to suit your needs. I hope all of this makes sense. I'm a bit tired, but wanted to get this up before bed.

  • filter
  • tag
  • email
Read More

sansmojo has posted 2 snippets.