| 84 | | = How to do Stuff = #HowTo |
| 85 | | |
| 86 | | == How do I create a subclass of an existing model? == #ModelInheritance |
| 87 | | |
| 88 | | Model inheritance (aka model subclassing) is available in Django trunk as of r7477. |
| 89 | | |
| 90 | | If you're using it, make sure you've [http://www.djangoproject.com/documentation/model-api/#model-inheritance read the documentation] and understand the difference between '''abstract base class''' and '''multi-table''' inheritance options. The latter can be confusing to newcomers because an instance of a parent class will also contain, on an attribute named after a child class, that same object represented as an instance of the child class. Got that? |
| 91 | | |
| 92 | | An established mechanism for adding additional information to Django `User` objects without subclassing is known as "profiles"; you can [http://www.djangobook.com/en/1.0/chapter12/#s-profiles read about it in the Definitive Guide]. |
| 93 | | |
| 94 | | For an important alternatve viewpoint on model subclassing, see: http://www.b-list.org/weblog/2007/02/20/about-model-subclassing |
| 95 | | |
| 96 | | == I think Ajax is awesome! How do I do Ajax with Django? == #Ajax |
| 97 | | |
| 98 | | Choose your favorite excellent Javascript library and go to it. Django provides serializers to JSON and XML, which you can read about in the documentation: http://www.djangoproject.com/documentation/serialization/ |
| 99 | | |
| 100 | | Also see this helpful article from James Bennett (with bonus anti-Javascript-helpers rant!): http://www.b-list.org/weblog/2006/07/02/django-and-ajax |
| 101 | | |
| 102 | | == How do I customise the admin interface so all logged-in users can use it without screwing up anything? == #AllUsersAdmin |
| 103 | | |
| 104 | | The admin interface is designed for use by trusted site staff, not by any user -- if you don't trust a user with the level of access the admin application provides, you'll need to provide non-admin views for the actions you'd like to allow them to take. |
| 105 | | |
| 106 | | == How do I make extensive changes in the admin interface? == #ExtensiveChangesAdmin |
| 107 | | |
| 108 | | At the moment it's probably best not to; the admin app is fairly specialized and doesn't have a lot of places to customize behavior, so you'll usually end up writing less code by just rolling your own set of views. The [http://code.djangoproject.com/wiki/NewformsAdminBranch newforms-admin branch], however, is significantly chanaging the admin app's interfaces to make customization much simpler and more flexible. |
| 109 | | |
| 110 | | For less extensive changes, also see the documentation in [http://www.djangobook.com/en/1.0/chapter17/ The Definitive Guide to Django]. |
| 111 | | |
| 112 | | == I want to have some code run when the server/application starts. How do I do that? == #ServerStartup |
| 113 | | |
| 114 | | Both mod_python and FastCGI are structured in such a way that there's no such thing as "application startup" or "server startup"; the best solution is to place your "startup" code somewhere that's guaranteed to be imported early on in the request/response cycle (the `__init__.py` file of your project, or of a specific application you're using, can be a good place, because Python will execute code found there the first time it has to import the module; just be aware that referencing the same module in different ways, say by doing `from myproject.myapp import foo` in one place, and `from myapp import foo` in another, will cause that code to be executed once for each different way you import it). |
| 115 | | |
| 116 | | == Do I have to hard-code my media URL in all my templates for CSS, images and Javascript? == #MediaURL |
| 117 | | |
| 118 | | No; you can use {{ MEDIA_URL }}. If you're using the development version of django and generic views, you can use {{ MEDIA_URL }} without changing anything. |
| 119 | | |
| 120 | | If you're using the development version and render_to_response(), you'll have to include RequestContext, as described here: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext |
| 121 | | |
| 122 | | If you're using 0.96, you can get the same functionality by creating a template context processor, as described: http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/ |
| 123 | | |
| 124 | | |
| 125 | | == How do I use Django in a shell script? == #Shell |
| 126 | | |
| 127 | | http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ |
| 128 | | |
| 129 | | == Can I use the Date/Time picker !JavaScript from the Admin in my own app? == #DatePicker |
| 130 | | Short answer: No. |
| 131 | | |
| 132 | | Long answer: It is possible, but requires reusing the Admin JS and CSS, and is more trouble than it is worth. Most !JavaScript frameworks (such as [http://developer.yahoo.com/yui/ YUI], [http://dojotoolkit.org/ Dojo], [http://jquery.com/ jQuery], and [http://www.prototypejs.org/ Prototype]) provide similar functionality, either out of the box or through plugins. Additionally, there are many !JavaScript snippets available across the Web. |
| 133 | | |
| 134 | | == I want to repeat a bit of dynamic information (eg from a database) on many views. Do I have to change every view? == |
| 135 | | |
| 136 | | No, you can use an [http://www.djangoproject.com/documentation/templates_python/#inclusion-tags inclusion tag]. |
| 137 | | |
| 138 | | == If I change my model, will {{{manage.py syncdb}}} update my database table? == #ModelChanges |
| 139 | | |
| 140 | | No, you'll need to manually change your database table. If you use `manage.py sqlall` on your app to produce a SQL file before editing your models, you can run it again afterwards and use the difference between the two to see what you need to change in the database. |
| 141 | | |
| | 155 | = How to do Stuff = |
| | 156 | |
| | 157 | == How do I create a subclass of an existing model? == #ModelInheritance |
| | 158 | |
| | 159 | Model inheritance (aka model subclassing) is available in Django trunk as of r7477. |
| | 160 | |
| | 161 | If you're using it, make sure you've [http://www.djangoproject.com/documentation/model-api/#model-inheritance read the documentation] and understand the difference between '''abstract base class''' and '''multi-table''' inheritance options. The latter can be confusing to newcomers because an instance of a parent class will also contain, on an attribute named after a child class, that same object represented as an instance of the child class. Got that? |
| | 162 | |
| | 163 | An established mechanism for adding additional information to Django `User` objects without subclassing is known as "profiles"; you can [http://www.djangobook.com/en/1.0/chapter12/#s-profiles read about it in the Definitive Guide]. |
| | 164 | |
| | 165 | For an important alternatve viewpoint on model subclassing, see: http://www.b-list.org/weblog/2007/02/20/about-model-subclassing |
| | 166 | |
| | 167 | == I think Ajax is awesome! How do I do Ajax with Django? == #Ajax |
| | 168 | |
| | 169 | Choose your favorite excellent Javascript library and go to it. Django provides serializers to JSON and XML, which you can read about in the documentation: http://www.djangoproject.com/documentation/serialization/ |
| | 170 | |
| | 171 | Also see this helpful article from James Bennett (with bonus anti-Javascript-helpers rant!): http://www.b-list.org/weblog/2006/07/02/django-and-ajax |
| | 172 | |
| | 173 | == How do I customise the admin interface so all logged-in users can use it without screwing up anything? == #AllUsersAdmin |
| | 174 | |
| | 175 | The admin interface is designed for use by trusted site staff, not by any user -- if you don't trust a user with the level of access the admin application provides, you'll need to provide non-admin views for the actions you'd like to allow them to take. |
| | 176 | |
| | 177 | == How do I make extensive changes in the admin interface? == #ExtensiveChangesAdmin |
| | 178 | |
| | 179 | At the moment it's probably best not to; the admin app is fairly specialized and doesn't have a lot of places to customize behavior, so you'll usually end up writing less code by just rolling your own set of views. The [http://code.djangoproject.com/wiki/NewformsAdminBranch newforms-admin branch], however, is significantly chanaging the admin app's interfaces to make customization much simpler and more flexible. |
| | 180 | |
| | 181 | For less extensive changes, also see the documentation in [http://www.djangobook.com/en/1.0/chapter17/ The Definitive Guide to Django]. |
| | 182 | |
| | 183 | == I want to have some code run when the server/application starts. How do I do that? == #ServerStartup |
| | 184 | |
| | 185 | Both mod_python and FastCGI are structured in such a way that there's no such thing as "application startup" or "server startup"; the best solution is to place your "startup" code somewhere that's guaranteed to be imported early on in the request/response cycle (the `__init__.py` file of your project, or of a specific application you're using, can be a good place, because Python will execute code found there the first time it has to import the module; just be aware that referencing the same module in different ways, say by doing `from myproject.myapp import foo` in one place, and `from myapp import foo` in another, will cause that code to be executed once for each different way you import it). |
| | 186 | |
| | 187 | == Do I have to hard-code my media URL in all my templates for CSS, images and Javascript? == #MediaURL |
| | 188 | |
| | 189 | No; you can use {{{{{ MEDIA_URL }}}}}. If you're using the development version of django and generic views, you can use {{{{{ MEDIA_URL }}}}} without changing anything. |
| | 190 | |
| | 191 | If you're using the development version and render_to_response(), you'll have to include RequestContext, as described here: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext |
| | 192 | |
| | 193 | If you're using 0.96, you can get the same functionality by creating a template context processor, as described: http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/ |
| | 194 | |
| | 195 | |
| | 196 | == How do I use Django in a shell script? == #Shell |
| | 197 | |
| | 198 | http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ |
| | 199 | |
| | 200 | == Can I use the Date/Time picker !JavaScript from the Admin in my own app? == #DatePicker |
| | 201 | Short answer: No. |
| | 202 | |
| | 203 | Long answer: It is possible, but requires reusing the Admin JS and CSS, and is more trouble than it is worth. Most !JavaScript frameworks (such as [http://developer.yahoo.com/yui/ YUI], [http://dojotoolkit.org/ Dojo], [http://jquery.com/ jQuery], and [http://www.prototypejs.org/ Prototype]) provide similar functionality, either out of the box or through plugins. Additionally, there are many !JavaScript snippets available across the Web. |
| | 204 | |
| | 205 | == I want to repeat a bit of dynamic information (eg from a database) on many views. Do I have to change every view? == |
| | 206 | |
| | 207 | No, you can use an [http://www.djangoproject.com/documentation/templates_python/#inclusion-tags inclusion tag]. |
| | 208 | |
| | 209 | == If I change my model, will {{{manage.py syncdb}}} update my database table? == #ModelChanges |
| | 210 | |
| | 211 | No, you'll need to manually change your database table. If you use `manage.py sqlall` on your app to produce a SQL file before editing your models, you can run it again afterwards and use the difference between the two to see what you need to change in the database. |
| | 212 | |
| | 213 | |